From: Ruediger Meier <sweet_f_a@gmx.de>
To: util-linux@vger.kernel.org
Subject: again errno usage, today: strutils.c
Date: Fri, 19 Feb 2016 12:06:36 +0100 [thread overview]
Message-ID: <201602191206.36635.sweet_f_a@gmx.de> (raw)
Hi,
There are many functions in strutils.c where we don't reset errno
before using it. For example this one:
unsigned long strtoul_or_err(const char *str, const char *errmesg)
{
unsigned long num;
char *end = NULL;
if (str == NULL || *str == '\0')
goto err;
errno = 0;
num = strtoul(str, &end, 10);
if (errno || str == end || (end && *end))
goto err;
return num;
err:
if (errno)
err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
}
You can make the problem visible:
$ ./logger --no-act -t "wtf" --id="XX" message
logger: failed to parse id: 'XX'
$ ./logger --no-act -t "wtf" --id="" message
logger: failed to parse id: '': No such file or directory
It's easy to fix:
+ errno = 0;
if (str == NULL || *str == '\0')
goto err;
- errno = 0;
But I want to ask whether it would be a good idea to make the message
generally less verbose:
- if (errno)
+ if (errno == ERANGE)
On other systems strtoul(3p) would return EINVAL too for case --id="XX"
but EINVAL has no useful information here:
test_logger: failed to parse id: 'XX': Invalid argument
This would not change anything on Linux/c99 as strtoul(3) only set
ERANGE there.
Opinions?
cu,
Rudi
next reply other threads:[~2016-02-19 11:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-19 11:06 Ruediger Meier [this message]
2016-02-19 16:02 ` again errno usage, today: strutils.c Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201602191206.36635.sweet_f_a@gmx.de \
--to=sweet_f_a@gmx.de \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox