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

Re: using long long and printf("%m") in debian



Wouter Verhelst <wouter@debian.org> writes:

> Bullshit. The above could also have been done as

> sprintf(buf, "Could not read file %s", filename)
> perror(buf);

There's a buffer overflow waiting to happen.

> or, perhaps (depending on what the file actually was):

> perror("Could not read configuration file");

> It's all a question of what exactly you specify as the string in the
> call to perror. Of course using __FUNCTION__ is hardly ever useful to
> the end-user, at least if you use it on its own. If properly used,
> however, there's nothing wrong with perror.

The main drawback to perror is that it doesn't use stdargs, which means
that while it's fine for those errors that don't take any variable data,
you have to jump through hoops to handle the other cases.  fprintf with
stderr works, but for succinctness, I prefer rolling my own:

static void
sysdie(const char *format, ...)
{
    int oerrno;
    va_list args;

    oerrno = errno;
    fflush(stdout);
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    fprintf(stderr, ": %s\n", strerror(oerrno));
    exit(1);
}

syswarn is the same without the exit.

Rolling one's own has the advantage of being portable.

-- 
Russ Allbery (rra@debian.org)               <http://www.eyrie.org/~eagle/>



Reply to: