From 7e8185ef942de5acecfa4cda03d7d7711ddda992 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Aug 2014 18:35:54 +0200 Subject: fix a couple of more lazy "return -1" Fix should strictly follow the rule to return negative errno-style error codes from functions, hence let's fix more "return -1"-style lazinesses. --- src/reply-password/reply-password.c | 2 +- src/shared/device-nodes.c | 4 ++-- src/shared/efivars.c | 2 +- src/shared/utf8.c | 45 +++++++++++++++++++++++-------------- src/shared/util.c | 8 +++---- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/reply-password/reply-password.c b/src/reply-password/reply-password.c index c730216b73..73c2d1bbdf 100644 --- a/src/reply-password/reply-password.c +++ b/src/reply-password/reply-password.c @@ -53,7 +53,7 @@ static int send_on_socket(int fd, const char *socket_name, const void *packet, s if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) { log_error("Failed to send: %m"); - return -1; + return -errno; } return 0; diff --git a/src/shared/device-nodes.c b/src/shared/device-nodes.c index 9837375099..73e9edd29d 100644 --- a/src/shared/device-nodes.c +++ b/src/shared/device-nodes.c @@ -41,7 +41,7 @@ int encode_devnode_name(const char *str, char *str_enc, size_t len) { size_t i, j; if (str == NULL || str_enc == NULL) - return -1; + return -EINVAL; for (i = 0, j = 0; str[i] != '\0'; i++) { int seqlen; @@ -70,5 +70,5 @@ int encode_devnode_name(const char *str, char *str_enc, size_t len) { str_enc[j] = '\0'; return 0; err: - return -1; + return -EINVAL; } diff --git a/src/shared/efivars.c b/src/shared/efivars.c index f18f5c4b16..a319574527 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -330,7 +330,7 @@ static int boot_id_hex(const char s[4]) { else if (s[i] >= 'A' && s[i] <= 'F') id |= (s[i] - 'A' + 10) << (3 - i) * 4; else - return -1; + return -EINVAL; return id; } diff --git a/src/shared/utf8.c b/src/shared/utf8.c index c559c13678..9353559b76 100644 --- a/src/shared/utf8.c +++ b/src/shared/utf8.c @@ -80,8 +80,11 @@ static bool is_unicode_control(uint32_t ch) { /* count of characters used to encode one unicode char */ static int utf8_encoded_expected_len(const char *str) { - unsigned char c = (unsigned char)str[0]; + unsigned char c; + assert(str); + + c = (unsigned char) str[0]; if (c < 0x80) return 1; if ((c & 0xe0) == 0xc0) @@ -94,16 +97,18 @@ static int utf8_encoded_expected_len(const char *str) { return 5; if ((c & 0xfe) == 0xfc) return 6; + return 0; } /* decode one unicode char */ int utf8_encoded_to_unichar(const char *str) { - int unichar; - int len; - int i; + int unichar, len, i; + + assert(str); len = utf8_encoded_expected_len(str); + switch (len) { case 1: return (int)str[0]; @@ -123,12 +128,12 @@ int utf8_encoded_to_unichar(const char *str) { unichar = (int)str[0] & 0x01; break; default: - return -1; + return -EINVAL; } for (i = 1; i < len; i++) { if (((int)str[i] & 0xc0) != 0x80) - return -1; + return -EINVAL; unichar <<= 6; unichar |= (int)str[i] & 0x3f; } @@ -142,10 +147,14 @@ bool utf8_is_printable_newline(const char* str, size_t length, bool newline) { assert(str); for (p = (const uint8_t*) str; length;) { - int encoded_len = utf8_encoded_valid_unichar((const char *)p); - int val = utf8_encoded_to_unichar((const char*)p); + int encoded_len, val; + + encoded_len = utf8_encoded_valid_unichar((const char *) p); + val = utf8_encoded_to_unichar((const char*) p); - if (encoded_len < 0 || val < 0 || is_unicode_control(val) || + if (encoded_len < 0 || + val < 0 || + is_unicode_control(val) || (!newline && val == '\n')) return false; @@ -165,7 +174,6 @@ const char *utf8_is_valid(const char *str) { int len; len = utf8_encoded_valid_unichar((const char *)p); - if (len < 0) return NULL; @@ -196,6 +204,7 @@ char *utf8_escape_invalid(const char *str) { str += 1; } } + *s = '\0'; return p; @@ -251,6 +260,7 @@ char *utf16_to_utf8(const void *s, size_t length) { /* expected size used to encode one unicode char */ static int utf8_unichar_to_encoded_len(int unichar) { + if (unichar < 0x80) return 1; if (unichar < 0x800) @@ -261,18 +271,19 @@ static int utf8_unichar_to_encoded_len(int unichar) { return 4; if (unichar < 0x4000000) return 5; + return 6; } /* validate one encoded unicode char and return its length */ int utf8_encoded_valid_unichar(const char *str) { - int len; - int unichar; - int i; + int len, unichar, i; + + assert(str); len = utf8_encoded_expected_len(str); if (len == 0) - return -1; + return -EINVAL; /* ascii is valid */ if (len == 1) @@ -281,17 +292,17 @@ int utf8_encoded_valid_unichar(const char *str) { /* check if expected encoded chars are available */ for (i = 0; i < len; i++) if ((str[i] & 0x80) != 0x80) - return -1; + return -EINVAL; unichar = utf8_encoded_to_unichar(str); /* check if encoded length matches encoded value */ if (utf8_unichar_to_encoded_len(unichar) != len) - return -1; + return -EINVAL; /* check if value has valid range */ if (!is_unicode_valid(unichar)) - return -1; + return -EINVAL; return len; } diff --git a/src/shared/util.c b/src/shared/util.c index 586f17af83..347fa12098 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1083,7 +1083,7 @@ int unhexchar(char c) { if (c >= 'A' && c <= 'F') return c - 'A' + 10; - return -1; + return -EINVAL; } char *hexmem(const void *p, size_t l) { @@ -1138,7 +1138,7 @@ int unoctchar(char c) { if (c >= '0' && c <= '7') return c - '0'; - return -1; + return -EINVAL; } char decchar(int x) { @@ -1150,7 +1150,7 @@ int undecchar(char c) { if (c >= '0' && c <= '9') return c - '0'; - return -1; + return -EINVAL; } char *cescape(const char *s) { @@ -4956,7 +4956,7 @@ int signal_from_string(const char *s) { if (signo > 0 && signo < _NSIG) return signo; } - return -1; + return -EINVAL; } bool kexec_loaded(void) { -- cgit v1.2.3-54-g00ecf