diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2009-04-15 21:47:04 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2009-04-15 21:47:04 +0200 |
commit | 3e5bafc98ad359f112d04071cfbb96a268a7ebc1 (patch) | |
tree | 58bed840f940c548f78c8ba03d4577eb793d299f /udev | |
parent | 9b144b88577e944659aa915cb27ff09af40223d2 (diff) |
libudev: path_encode - always return 0 if encoded string does not fit into size
Diffstat (limited to 'udev')
-rw-r--r-- | udev/lib/libudev-util.c | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/udev/lib/libudev-util.c b/udev/lib/libudev-util.c index a40be06757..243a99dce9 100644 --- a/udev/lib/libudev-util.c +++ b/udev/lib/libudev-util.c @@ -101,12 +101,12 @@ int util_log_priority(const char *priority) return 0; } -size_t util_path_encode(char *s, size_t len) +size_t util_path_encode(char *s, size_t size) { - char t[(len * 4)+1]; + char t[(size * 4)+1]; size_t i, j; - for (i = 0, j = 0; s[i] != '\0'; i++) { + for (i = 0, j = 0; s[i] != '\0' && i < size; i++) { if (s[i] == '/') { memcpy(&t[j], "\\x2f", 4); j += 4; @@ -118,11 +118,12 @@ size_t util_path_encode(char *s, size_t len) j++; } } - if (len == 0) - return j; - i = (j < len - 1) ? j : len - 1; - memcpy(s, t, i); - s[i] = '\0'; + if (i >= size) + return 0; + if (j >= size) + return 0; + memcpy(s, t, j); + s[j] = '\0'; return j; } @@ -134,7 +135,7 @@ size_t util_path_decode(char *s) if (memcmp(&s[i], "\\x2f", 4) == 0) { s[j] = '/'; i += 4; - }else if (memcmp(&s[i], "\\x5c", 4) == 0) { + } else if (memcmp(&s[i], "\\x5c", 4) == 0) { s[j] = '\\'; i += 4; } else { |