diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-06-09 13:41:24 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-06-09 13:41:24 +0200 |
commit | 294a3121aac8ca8bdadbf4f826ec29e2702cd646 (patch) | |
tree | 969c21c101ec7c23a00cc713e067638781d09531 /src/basic/utf8.c | |
parent | lldp: check that lldp neighbor raw data size is in expected range (diff) | |
download | systemd-294a3121aac8ca8bdadbf4f826ec29e2702cd646.tar.xz systemd-294a3121aac8ca8bdadbf4f826ec29e2702cd646.zip |
basic/utf8: add ascii_is_valid_n()
Diffstat (limited to 'src/basic/utf8.c')
-rw-r--r-- | src/basic/utf8.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/basic/utf8.c b/src/basic/utf8.c index 670a98a6a9..8bef153968 100644 --- a/src/basic/utf8.c +++ b/src/basic/utf8.c @@ -247,6 +247,9 @@ char *utf8_escape_non_printable(const char *str) { char *ascii_is_valid(const char *str) { const char *p; + /* Check whether the string consists of valid ASCII bytes, + * i.e values between 0 and 127, inclusive. */ + assert(str); for (p = str; *p; p++) @@ -256,6 +259,21 @@ char *ascii_is_valid(const char *str) { return (char*) str; } +char *ascii_is_valid_n(const char *str, size_t len) { + size_t i; + + /* Very similar to ascii_is_valid(), but checks exactly len + * bytes and rejects any NULs in that range. */ + + assert(str); + + for (i = 0; i < len; i++) + if ((unsigned char) str[i] >= 128 || str[i] == 0) + return NULL; + + return (char*) str; +} + /** * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8 * @out_utf8: output buffer of at least 4 bytes or NULL |