diff options
author | Patrick Uiterwijk <patrick@puiterwijk.org> | 2018-02-22 19:41:30 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-02-22 19:41:30 +0100 |
commit | 5547c12503a683290eaed47954ffcfb2d1bc03cd (patch) | |
tree | bd7847c9bc76231abf3aa71c596e935e4d6470a4 /src/basic/parse-util.c | |
parent | 6afe9046d570fd86dbc04526b5685c64d7958086 (diff) | |
download | systemd-5547c12503a683290eaed47954ffcfb2d1bc03cd.tar.gz systemd-5547c12503a683290eaed47954ffcfb2d1bc03cd.tar.bz2 systemd-5547c12503a683290eaed47954ffcfb2d1bc03cd.zip |
Fix format-truncation compile failure by typecasting USB IDs (#8250)
This patch adds safe_atoux16 for parsing an unsigned hexadecimal 16bit int, and
uses that for parsing USB device and vendor IDs.
This fixes a compile error with gcc-8 because while we know that USB IDs are 2 bytes,
the compiler does not know that.
../src/udev/udev-builtin-hwdb.c:80:38: error: '%04X' directive output may be
truncated writing between 4 and 8 bytes into a region of size between 2 and 6
[-Werror=format-truncation=]
Signed-off-by: Adam Williamson <awilliam@redhat.com>
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r-- | src/basic/parse-util.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 2c22753dea..fa5b4a353a 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -531,6 +531,30 @@ int safe_atoi16(const char *s, int16_t *ret) { return 0; } +int safe_atoux16(const char *s, uint16_t *ret) { + char *x = NULL; + unsigned long l; + + assert(s); + assert(ret); + + s += strspn(s, WHITESPACE); + + errno = 0; + l = strtoul(s, &x, 16); + if (errno > 0) + return -errno; + if (!x || x == s || *x != 0) + return -EINVAL; + if (s[0] == '-') + return -ERANGE; + if ((unsigned long) (uint16_t) l != l) + return -ERANGE; + + *ret = (uint16_t) l; + return 0; +} + int safe_atod(const char *s, double *ret_d) { _cleanup_(freelocalep) locale_t loc = (locale_t) 0; char *x = NULL; |