diff options
author | Thomas Haller <thaller@redhat.com> | 2019-02-21 12:03:29 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-02-22 10:43:22 +0100 |
commit | 299147d2f00f0a396e19edc95774c1e6a07f8ba7 (patch) | |
tree | fda1c0f27745a7431b0206846ebab1cc2898f1d2 | |
parent | b1fd133aa4b45c476bf5c0d7a134f6eb11a560c0 (diff) | |
download | NetworkManager-299147d2f00f0a396e19edc95774c1e6a07f8ba7.tar.gz |
shared: support empty blobs in nm_utils_bin2hexstr_full()
The limitation to not accept a length of 0 is easy to forget.
Handle also empty blobs in a sensible way, by returning the
empty word.
-rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 738b4ad518..0e427a24c6 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -2538,8 +2538,10 @@ nm_utils_memeqzero (gconstpointer data, gsize length) /** * nm_utils_bin2hexstr_full: - * @addr: pointer of @length bytes. - * @length: number of bytes in @addr + * @addr: pointer of @length bytes. If @length is zero, this may + * also be %NULL. + * @length: number of bytes in @addr. May also be zero, in which + * case this will return an empty string. * @delimiter: either '\0', otherwise the output string will have the * given delimiter character between each two hex numbers. * @upper_case: if TRUE, use upper case ASCII characters for hex. @@ -2565,9 +2567,6 @@ nm_utils_bin2hexstr_full (gconstpointer addr, const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef"; char *out0; - nm_assert (addr); - nm_assert (length > 0); - if (out) out0 = out; else { @@ -2579,19 +2578,22 @@ nm_utils_bin2hexstr_full (gconstpointer addr, /* @out must contain at least @length*3 bytes if @delimiter is set, * otherwise, @length*2+1. */ - for (;;) { - const guint8 v = *in++; + if (length > 0) { + nm_assert (in); + for (;;) { + const guint8 v = *in++; - *out++ = LOOKUP[v >> 4]; - *out++ = LOOKUP[v & 0x0F]; - length--; - if (!length) - break; - if (delimiter) - *out++ = delimiter; + *out++ = LOOKUP[v >> 4]; + *out++ = LOOKUP[v & 0x0F]; + length--; + if (!length) + break; + if (delimiter) + *out++ = delimiter; + } } - *out = 0; + *out = '\0'; return out0; } |