Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | From c2b3ebe112ebfd9f9e82fb1531ee225c3152ca83 Mon Sep 17 00:00:00 2001 |
| 2 | From: Patrick Uiterwijk <patrick@puiterwijk.org> |
| 3 | Date: Thu, 22 Feb 2018 19:41:30 +0100 |
| 4 | Subject: [PATCH] Fix format-truncation compile failure by typecasting USB IDs |
| 5 | (#8250) |
| 6 | |
| 7 | This patch adds safe_atoux16 for parsing an unsigned hexadecimal 16bit int, and |
| 8 | uses that for parsing USB device and vendor IDs. |
| 9 | |
| 10 | This fixes a compile error with gcc-8 because while we know that USB IDs are 2 bytes, |
| 11 | the compiler does not know that. |
| 12 | |
| 13 | ../src/udev/udev-builtin-hwdb.c:80:38: error: '%04X' directive output may be |
| 14 | truncated writing between 4 and 8 bytes into a region of size between 2 and 6 |
| 15 | [-Werror=format-truncation=] |
| 16 | |
| 17 | Upstream-Status: Backport [https://github.com/systemd/systemd/commit/5547c12503a683290eaed47954ffcfb2d1bc03cd] |
| 18 | |
| 19 | Signed-off-by: Adam Williamson <awilliam@redhat.com> |
| 20 | Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com> |
| 21 | --- |
| 22 | src/basic/parse-util.c | 24 ++++++++++++++++++++++ |
| 23 | src/basic/parse-util.h | 2 ++ |
| 24 | src/test/test-parse-util.c | 39 ++++++++++++++++++++++++++++++++++++ |
| 25 | src/udev/udev-builtin-hwdb.c | 13 ++++++------ |
| 26 | 4 files changed, 71 insertions(+), 7 deletions(-) |
| 27 | |
| 28 | diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c |
| 29 | index 97533721d..ff3fc298a 100644 |
| 30 | --- a/src/basic/parse-util.c |
| 31 | +++ b/src/basic/parse-util.c |
| 32 | @@ -532,6 +532,30 @@ int safe_atoi16(const char *s, int16_t *ret) { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | +int safe_atoux16(const char *s, uint16_t *ret) { |
| 37 | + char *x = NULL; |
| 38 | + unsigned long l; |
| 39 | + |
| 40 | + assert(s); |
| 41 | + assert(ret); |
| 42 | + |
| 43 | + s += strspn(s, WHITESPACE); |
| 44 | + |
| 45 | + errno = 0; |
| 46 | + l = strtoul(s, &x, 16); |
| 47 | + if (errno > 0) |
| 48 | + return -errno; |
| 49 | + if (!x || x == s || *x != 0) |
| 50 | + return -EINVAL; |
| 51 | + if (s[0] == '-') |
| 52 | + return -ERANGE; |
| 53 | + if ((unsigned long) (uint16_t) l != l) |
| 54 | + return -ERANGE; |
| 55 | + |
| 56 | + *ret = (uint16_t) l; |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | int safe_atod(const char *s, double *ret_d) { |
| 61 | _cleanup_(freelocalep) locale_t loc = (locale_t) 0; |
| 62 | char *x = NULL; |
| 63 | diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h |
| 64 | index 1eda1d7f9..727422056 100644 |
| 65 | --- a/src/basic/parse-util.h |
| 66 | +++ b/src/basic/parse-util.h |
| 67 | @@ -54,6 +54,8 @@ int safe_atou8(const char *s, uint8_t *ret); |
| 68 | int safe_atou16(const char *s, uint16_t *ret); |
| 69 | int safe_atoi16(const char *s, int16_t *ret); |
| 70 | |
| 71 | +int safe_atoux16(const char *s, uint16_t *ret); |
| 72 | + |
| 73 | static inline int safe_atou32(const char *s, uint32_t *ret_u) { |
| 74 | assert_cc(sizeof(uint32_t) == sizeof(unsigned)); |
| 75 | return safe_atou(s, (unsigned*) ret_u); |
| 76 | diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c |
| 77 | index 937500213..a99cea5a1 100644 |
| 78 | --- a/src/test/test-parse-util.c |
| 79 | +++ b/src/test/test-parse-util.c |
| 80 | @@ -468,6 +468,44 @@ static void test_safe_atoi16(void) { |
| 81 | assert_se(r == -EINVAL); |
| 82 | } |
| 83 | |
| 84 | +static void test_safe_atoux16(void) { |
| 85 | + int r; |
| 86 | + uint16_t l; |
| 87 | + |
| 88 | + r = safe_atoux16("1234", &l); |
| 89 | + assert_se(r == 0); |
| 90 | + assert_se(l == 0x1234); |
| 91 | + |
| 92 | + r = safe_atoux16("abcd", &l); |
| 93 | + assert_se(r == 0); |
| 94 | + assert_se(l == 0xabcd); |
| 95 | + |
| 96 | + r = safe_atoux16(" 1234", &l); |
| 97 | + assert_se(r == 0); |
| 98 | + assert_se(l == 0x1234); |
| 99 | + |
| 100 | + r = safe_atoux16("12345", &l); |
| 101 | + assert_se(r == -ERANGE); |
| 102 | + |
| 103 | + r = safe_atoux16("-1", &l); |
| 104 | + assert_se(r == -ERANGE); |
| 105 | + |
| 106 | + r = safe_atoux16(" -1", &l); |
| 107 | + assert_se(r == -ERANGE); |
| 108 | + |
| 109 | + r = safe_atoux16("junk", &l); |
| 110 | + assert_se(r == -EINVAL); |
| 111 | + |
| 112 | + r = safe_atoux16("123x", &l); |
| 113 | + assert_se(r == -EINVAL); |
| 114 | + |
| 115 | + r = safe_atoux16("12.3", &l); |
| 116 | + assert_se(r == -EINVAL); |
| 117 | + |
| 118 | + r = safe_atoux16("", &l); |
| 119 | + assert_se(r == -EINVAL); |
| 120 | +} |
| 121 | + |
| 122 | static void test_safe_atou64(void) { |
| 123 | int r; |
| 124 | uint64_t l; |
| 125 | @@ -745,6 +783,7 @@ int main(int argc, char *argv[]) { |
| 126 | test_safe_atolli(); |
| 127 | test_safe_atou16(); |
| 128 | test_safe_atoi16(); |
| 129 | + test_safe_atoux16(); |
| 130 | test_safe_atou64(); |
| 131 | test_safe_atoi64(); |
| 132 | test_safe_atod(); |
| 133 | diff --git a/src/udev/udev-builtin-hwdb.c b/src/udev/udev-builtin-hwdb.c |
| 134 | index ca7f7c230..dbfe02429 100644 |
| 135 | --- a/src/udev/udev-builtin-hwdb.c |
| 136 | +++ b/src/udev/udev-builtin-hwdb.c |
| 137 | @@ -27,6 +27,7 @@ |
| 138 | |
| 139 | #include "alloc-util.h" |
| 140 | #include "hwdb-util.h" |
| 141 | +#include "parse-util.h" |
| 142 | #include "string-util.h" |
| 143 | #include "udev-util.h" |
| 144 | #include "udev.h" |
| 145 | @@ -63,7 +64,7 @@ int udev_builtin_hwdb_lookup(struct udev_device *dev, |
| 146 | |
| 147 | static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) { |
| 148 | const char *v, *p; |
| 149 | - int vn, pn; |
| 150 | + uint16_t vn, pn; |
| 151 | |
| 152 | v = udev_device_get_sysattr_value(dev, "idVendor"); |
| 153 | if (!v) |
| 154 | @@ -71,12 +72,10 @@ static const char *modalias_usb(struct udev_device *dev, char *s, size_t size) { |
| 155 | p = udev_device_get_sysattr_value(dev, "idProduct"); |
| 156 | if (!p) |
| 157 | return NULL; |
| 158 | - vn = strtol(v, NULL, 16); |
| 159 | - if (vn <= 0) |
| 160 | - return NULL; |
| 161 | - pn = strtol(p, NULL, 16); |
| 162 | - if (pn <= 0) |
| 163 | - return NULL; |
| 164 | + if (safe_atoux16(v, &vn) < 0) |
| 165 | + return NULL; |
| 166 | + if (safe_atoux16(p, &pn) < 0) |
| 167 | + return NULL; |
| 168 | snprintf(s, size, "usb:v%04Xp%04X*", vn, pn); |
| 169 | return s; |
| 170 | } |
| 171 | -- |
| 172 | 2.17.0 |
| 173 | |