>OpenBSD's documentation for openlog's first paramater 'ident' is less clear
>than Debian [1] or GNU [2] that the memory pointed to must remain valid for
>as long as syslog is called (which I'm assuming without hard evidence is
>equivalent to "until closelog is called").
>
>Although this isn't specified by POSIX, on both OpenBSD and Debian passing a
>pointer to memory that is then free'd causes random bytes to be written to
>syslog. This tiny program demonstrates the problem:
>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <syslog.h>
>
>  int main() {
>      char *n = malloc(128);
>      strcpy(n, "name");
>      openlog(n, LOG_CONS, LOG_DAEMON);
>      free(n);
>      syslog(LOG_ERR, "msg");
>      return 0;
>  }

How did you arrive at writing code like this??

ident is a string, or pointer to some bytes.  It says that is used
by every syslog() call.  Why would you think to free it?  Why would
you assume it can be freed, where comes this assumption that openlog
would "save a copy of it"?  That is sheer madness.

>Most of the time this leads to rubbish being sent to /var/log/daemon: once
>in a while it will segfault. Removing the free() call fixes the problem.
>
>The patch at the end of this email is one possible suggestion for making this
>clear, but it's difficult to do so succinctly.

If we are going to document it, about 6 word adjustment speaking about
"storage", "lifetime", or "persisting" "until closelog()" should be enough,
You are falling into the trap of documenting what goes wrong rather than
how it works.

The parameter ident is a string that will be prepended to every
message, it's storage must persist until
Fn closelog .

Reply via email to