summaryrefslogtreecommitdiff
path: root/src/libsystemd-network
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-01-20 15:36:09 +0100
committerLennart Poettering <lennart@poettering.net>2023-01-21 10:45:25 +0100
commit7153213e406815ae0083789c211d8b77c79588d5 (patch)
treeae980516803b4a2bb512d9ffaa88148e66f1ec62 /src/libsystemd-network
parent9e2e3342ca5d219085c8f00ef0c2209f0d9b48ec (diff)
downloadsystemd-7153213e406815ae0083789c211d8b77c79588d5.tar.gz
string-util: add common implementation of function that converts sized character buffers to NUL terminated C strings
Diffstat (limited to 'src/libsystemd-network')
-rw-r--r--src/libsystemd-network/dhcp-option.c11
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c11
2 files changed, 9 insertions, 13 deletions
diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c
index 7f49da7c2d..a52259c238 100644
--- a/src/libsystemd-network/dhcp-option.c
+++ b/src/libsystemd-network/dhcp-option.c
@@ -279,6 +279,7 @@ static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overlo
uint8_t code, len;
const uint8_t *option;
size_t offset = 0;
+ int r;
while (offset < buflen) {
code = options[offset ++];
@@ -318,13 +319,9 @@ static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overlo
if (error_message) {
_cleanup_free_ char *string = NULL;
- /* Accept a trailing NUL byte */
- if (memchr(option, 0, len - 1))
- return -EINVAL;
-
- string = memdup_suffix0((const char *) option, len);
- if (!string)
- return -ENOMEM;
+ r = make_cstring((const char*) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
+ if (r < 0)
+ return r;
if (!ascii_is_valid(string))
return -EINVAL;
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index b14ad57071..02c6a6e0d7 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -376,6 +376,8 @@ static int lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) {
}
static int lease_parse_string(const uint8_t *option, size_t len, char **ret) {
+ int r;
+
assert(option);
assert(ret);
@@ -388,12 +390,9 @@ static int lease_parse_string(const uint8_t *option, size_t len, char **ret) {
* One trailing NUL byte is OK, we don't mind. See:
* https://github.com/systemd/systemd/issues/1337
*/
- if (memchr(option, 0, len - 1))
- return -EINVAL;
-
- string = memdup_suffix0((const char *) option, len);
- if (!string)
- return -ENOMEM;
+ r = make_cstring((const char*) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
+ if (r < 0)
+ return r;
free_and_replace(*ret, string);
}