summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-02-21 12:03:29 +0100
committerThomas Haller <thaller@redhat.com>2019-02-22 14:04:13 +0100
commit9d0da3e60bbb9e5fb209eeec572adfa29b2cbce2 (patch)
tree0c985c05e0b42b707eb915ab45ec87b3db012b80
parent53b747fff5cb6a8ca6a975a31b697507a13e418e (diff)
downloadNetworkManager-9d0da3e60bbb9e5fb209eeec572adfa29b2cbce2.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.c32
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;
}