groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use `strsave()`, not `strdup()`.


From: Ralph Corderoy
Subject: Re: Use `strsave()`, not `strdup()`.
Date: Mon, 08 Nov 2021 14:05:00 +0000

Hi Ingo,

>  3. Using your own replacement functions for Standard C and/or
>     POSIX functions is a bad idea because:
>
>  3.1. It makes the code harder to read and audit for experienced
>       programmers.  Instead of being able to use their knowledge
>       of standards, they have to figure out what your private
>       functions do and how they are meant to be used, slowing
>       down the process of understanding the code

True in general.  We all hate wading through layers of needless
abstraction which is a waste of effort to memorise.  But an accepted
exception is a function which provides the same interface as the
standard one except it never returns an error.  Instead it will cause
the program to exit.  These must be named after the original to aid
comprehension: malloc(3) is typically wrapped as xmalloc(3) or
emalloc(3).

Instead of returning an error, the function can write(2) a diagnostic to
stderr and abort(3) to give the user something to diagnose.  This isn't
as good as error returns rippling all the way up the call stack,
carefully handled appropriately at each level, but for gradual
improvement of legacy code it's achievable.  Analysing all call sites,
correctly determine the right action, having regression tests to confirm
accuracy, etc., means the improvement never happens.

It doesn't matter that a ‘library’ call aborts in groff's case as it's
not like ed(1) losing one's magnum opus by aborting.  And by moving to
estrdup(), say, then all the sites which need examining in a future
round of improvement can be easily found compared with sticking to
strdup() and some of them having been vetted and some not.

-- 
Cheers, Ralph.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]