[PATCH 2 of 3] chg: add util function abortmsge to print error with errno

Danek Duvall danek.duvall at oracle.com
Tue Apr 5 11:33:25 EDT 2016

Jun Wu wrote:

> -void abortmsg(const char *fmt, ...)
> +void abortmsge(const char *fmt, ...)
>  {
>  	va_list args;
>  	va_start(args, fmt);
>  	fsetcolor(stderr, "1;31");
>  	fputs("chg: abort: ", stderr);
>  	vfprintf(stderr, fmt, args);
> +	if (errno != 0)
> +		fprintf(stderr, " (errno = %d, %s)", errno, strerror(errno));

At least vfprintf() can fail, setting errno; I'm pretty sure fputs() can,
too.  I don't know about fsetcolor(), but I'd suspect so.

So you probably need to preserve errno first thing coming in to the
function, and then check that, or errno might not be what you think it is.
Either that, or pass it in as the first argument, but that'll take more
restructuring of the code.


More information about the Mercurial-devel mailing list