Re: [PATCH] diesys calls die and also reports strerror(errno)
- From
Jeff King <peff@peff.net>
- Date
- Jun 4, 2009, 20:50 UTC
- Message-ID
- <20090604205055.GB17478@sigill.intra.peff.net>
- In-Reply-To
- <1244081105-7149-1-git-send-email-aspotashev@gmail.com>
On Thu, Jun 04, 2009 at 06:05:05AM +0400, Alexander Potashev wrote:
Show 7 quoted lines
> Firstly I was going to write a 'adapt_to_fmt' function which would > double all inclusions of '%', and then use it for strerror(err) and > make printf-like functions happy (actually 'die_routine'). > > BUT, Have you ever seen an error description containing '%'? I haven't. > So, handling the case of '%'s is not worth injecting several dump lines > into the sources of the Beatiful Content Tracker.
That makes me a little nervous. No, I don't think there are any '%' signs in standard 'C' locale messages (at least not in GNU libc). But what about other locales, especially ones which use a multi-byte encoding?
Though your code does at least recognize the situation and does something sane instead of feeding bogus parameters to fprintf.
Show 17 quoted lines
> +void diesys(const char *err, ...)
> +{
> + va_list params;
> + char *fullfmt;
> + const char *strerr;
> +
> + va_start(params, err);
> +
> + strerr = strerror(errno);
> + if (strchr(strerr, '%'))
> + strerr = "<error description contains '%%'>";
> + fullfmt = xmalloc(strlen(err) + strlen(strerr) + 3);
> + sprintf(fullfmt, "%s: %s", err, strerr);
> + die_routine(fullfmt, params);
> +
> + va_end(params);
> +}Should we be calling malloc here? One of the possible error conditions is that we're out of memory (though xmalloc itself just uses "die"). I don't think there is a good reason not to use a reasonably-sized static buffer, which should increase robustness (report() is already using a 1024-character buffer, so any message would be truncated there anyway).
-Peff