diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/support/json.c | 6 | ||||
-rw-r--r-- | src/util/support/k5buf.c | 27 | ||||
-rw-r--r-- | src/util/support/libkrb5support-fixed.exports | 1 | ||||
-rw-r--r-- | src/util/support/t_k5buf.c | 17 | ||||
-rw-r--r-- | src/util/support/utf8_conv.c | 4 |
5 files changed, 28 insertions, 27 deletions
diff --git a/src/util/support/json.c b/src/util/support/json.c index ae2feae..ac2e5be 100644 --- a/src/util/support/json.c +++ b/src/util/support/json.c @@ -696,10 +696,8 @@ k5_json_encode(k5_json_value val, char **json_out) k5_buf_free(&buf); return ret; } - if (k5_buf_status(&buf) != 0) - return ENOMEM; - *json_out = buf.data; - return 0; + *json_out = k5_buf_cstring(&buf); + return (*json_out == NULL) ? ENOMEM : 0; } /*** JSON decoding ***/ diff --git a/src/util/support/k5buf.c b/src/util/support/k5buf.c index b2b5e5b..a17d231 100644 --- a/src/util/support/k5buf.c +++ b/src/util/support/k5buf.c @@ -73,13 +73,13 @@ ensure_space(struct k5buf *buf, size_t len) if (buf->buftype == K5BUF_ERROR) return 0; - if (buf->space - 1 - buf->len >= len) /* Enough room already. */ + if (buf->space - buf->len >= len) /* Enough room already. */ return 1; if (buf->buftype == K5BUF_FIXED) /* Can't resize a fixed buffer. */ goto error_exit; assert(buf->buftype == K5BUF_DYNAMIC || buf->buftype == K5BUF_DYNAMIC_ZAP); new_space = buf->space * 2; - while (new_space - buf->len - 1 < len) { + while (new_space - buf->len < len) { if (new_space > SIZE_MAX / 2) goto error_exit; new_space *= 2; @@ -90,7 +90,6 @@ ensure_space(struct k5buf *buf, size_t len) if (new_data == NULL) goto error_exit; memcpy(new_data, buf->data, buf->len); - new_data[buf->len] = '\0'; zap(buf->data, buf->len); free(buf->data); } else { @@ -112,14 +111,13 @@ error_exit: } void -k5_buf_init_fixed(struct k5buf *buf, char *data, size_t space) +k5_buf_init_fixed(struct k5buf *buf, void *data, size_t space) { assert(space > 0); buf->buftype = K5BUF_FIXED; buf->data = data; buf->space = space; buf->len = 0; - *endptr(buf) = '\0'; } void @@ -133,7 +131,6 @@ k5_buf_init_dynamic(struct k5buf *buf) return; } buf->len = 0; - *endptr(buf) = '\0'; } void @@ -158,7 +155,6 @@ k5_buf_add_len(struct k5buf *buf, const void *data, size_t len) if (len > 0) memcpy(endptr(buf), data, len); buf->len += len; - *endptr(buf) = '\0'; } void @@ -195,7 +191,7 @@ k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap) if (r >= 0) { /* snprintf correctly told us how much space is required. */ - if (!ensure_space(buf, r)) + if (!ensure_space(buf, r + 1)) return; remaining = buf->space - buf->len; r = vsnprintf(endptr(buf), remaining, fmt, ap); @@ -214,8 +210,8 @@ k5_buf_add_vfmt(struct k5buf *buf, const char *fmt, va_list ap) return; } if (ensure_space(buf, r)) { - /* Copy the temporary string into buf, including terminator. */ - memcpy(endptr(buf), tmp, r + 1); + /* Copy the temporary string into buf. */ + memcpy(endptr(buf), tmp, r); buf->len += r; } if (buf->buftype == K5BUF_DYNAMIC_ZAP) @@ -233,13 +229,21 @@ k5_buf_add_fmt(struct k5buf *buf, const char *fmt, ...) va_end(ap); } +char * +k5_buf_cstring(struct k5buf *buf) +{ + if (!ensure_space(buf, 1)) + return NULL; + *endptr(buf) = '\0'; + return buf->data; +} + void * k5_buf_get_space(struct k5buf *buf, size_t len) { if (!ensure_space(buf, len)) return NULL; buf->len += len; - *endptr(buf) = '\0'; return endptr(buf) - len; } @@ -250,7 +254,6 @@ k5_buf_truncate(struct k5buf *buf, size_t len) return; assert(len <= buf->len); buf->len = len; - *endptr(buf) = '\0'; } int diff --git a/src/util/support/libkrb5support-fixed.exports b/src/util/support/libkrb5support-fixed.exports index 0bafe1c..0158024 100644 --- a/src/util/support/libkrb5support-fixed.exports +++ b/src/util/support/libkrb5support-fixed.exports @@ -8,6 +8,7 @@ k5_buf_add k5_buf_add_len k5_buf_add_fmt k5_buf_add_vfmt +k5_buf_cstring k5_buf_get_space k5_buf_truncate k5_buf_status diff --git a/src/util/support/t_k5buf.c b/src/util/support/t_k5buf.c index ba86851..734b272 100644 --- a/src/util/support/t_k5buf.c +++ b/src/util/support/t_k5buf.c @@ -50,7 +50,6 @@ check_buf(struct k5buf *buf, const char *name) } else { fail_if(buf->space == 0, name); fail_if(buf->len >= buf->space, name); - fail_if(((char *)buf->data)[buf->len] != 0, name); } } @@ -65,14 +64,14 @@ test_basic() k5_buf_add_len(&buf, "world", 5); check_buf(&buf, "basic fixed"); fail_if(buf.data == NULL || buf.len != 11, "basic fixed"); - fail_if(strcmp(buf.data, "Hello world") != 0, "basic fixed"); + fail_if(memcmp(buf.data, "Hello world", 11) != 0, "basic fixed"); k5_buf_init_dynamic(&buf); k5_buf_add_len(&buf, "Hello", 5); k5_buf_add(&buf, " world"); check_buf(&buf, "basic dynamic"); fail_if(buf.data == NULL || buf.len != 11, "basic dynamic"); - fail_if(strcmp(buf.data, "Hello world") != 0, "basic dynamic"); + fail_if(memcmp(buf.data, "Hello world", 11) != 0, "basic dynamic"); k5_buf_free(&buf); } @@ -141,7 +140,7 @@ test_overflow() /* Cause a fixed-sized buffer overflow. */ k5_buf_init_fixed(&buf, storage, sizeof(storage)); k5_buf_add(&buf, "12345"); - k5_buf_add(&buf, "12345"); + k5_buf_add(&buf, "123456"); check_buf(&buf, "overflow 1"); fail_if(buf.buftype != K5BUF_ERROR, "overflow 1"); @@ -161,7 +160,7 @@ test_error() /* Cause an overflow and then perform actions afterwards. */ k5_buf_init_fixed(&buf, storage, sizeof(storage)); - k5_buf_add(&buf, "1"); + k5_buf_add(&buf, "12"); fail_if(buf.buftype != K5BUF_ERROR, "error"); check_buf(&buf, "error"); k5_buf_add(&buf, "test"); @@ -184,7 +183,7 @@ test_truncate() k5_buf_truncate(&buf, 7); check_buf(&buf, "truncate"); fail_if(buf.data == NULL || buf.len != 7, "truncate"); - fail_if(strcmp(buf.data, "abcdefg") != 0, "truncate"); + fail_if(memcmp(buf.data, "abcdefg", 7) != 0, "truncate"); k5_buf_free(&buf); } @@ -222,7 +221,7 @@ test_fmt() k5_buf_add_fmt(&buf, " %d ", 3); check_buf(&buf, "fmt 1"); fail_if(buf.data == NULL || buf.len != 6, "fmt 1"); - fail_if(strcmp(buf.data, "foo 3 ") != 0, "fmt 1"); + fail_if(memcmp(buf.data, "foo 3 ", 6) != 0, "fmt 1"); /* Overflow the same buffer with formatted text. */ k5_buf_add_fmt(&buf, "%d%d%d%d", 1, 2, 3, 4); @@ -235,14 +234,14 @@ test_fmt() k5_buf_add_fmt(&buf, " %d ", 3); check_buf(&buf, "fmt 3"); fail_if(buf.data == NULL || buf.len != 6, "fmt 3"); - fail_if(strcmp(buf.data, "foo 3 ") != 0, "fmt 3"); + fail_if(memcmp(buf.data, "foo 3 ", 6) != 0, "fmt 3"); /* Format more text into the same buffer, causing a big resize. */ k5_buf_add_fmt(&buf, "%s", data); check_buf(&buf, "fmt 4"); fail_if(buf.space != 2048, "fmt 4"); fail_if(buf.data == NULL || buf.len != 1029, "fmt 4"); - fail_if(strcmp((char *)buf.data + 6, data) != 0, "fmt 4"); + fail_if(memcmp((char *)buf.data + 6, data, 1023) != 0, "fmt 4"); k5_buf_free(&buf); } diff --git a/src/util/support/utf8_conv.c b/src/util/support/utf8_conv.c index 5ddaa2d..926a3c8 100644 --- a/src/util/support/utf8_conv.c +++ b/src/util/support/utf8_conv.c @@ -191,8 +191,8 @@ k5_utf16le_to_utf8(const uint8_t *utf16bytes, size_t nbytes, char **utf8_out) if (in.status) goto invalid; - *utf8_out = buf.data; - return 0; + *utf8_out = k5_buf_cstring(&buf); + return (*utf8_out == NULL) ? ENOMEM : 0; invalid: k5_buf_free(&buf); |