diff options
| author | Greg Hudson <ghudson@mit.edu> | 2013-04-08 15:32:31 -0400 |
|---|---|---|
| committer | Greg Hudson <ghudson@mit.edu> | 2013-04-08 15:32:31 -0400 |
| commit | 31124ffb81e8c0935403a9fdc169dead5ecaa777 (patch) | |
| tree | 837d49e7ef8de324f8ad288ab3231ca2acdcdbd7 /src/util/support | |
| parent | caaf72893a5be61822763eb471f4d573992479ed (diff) | |
| download | krb5-31124ffb81e8c0935403a9fdc169dead5ecaa777.tar.gz krb5-31124ffb81e8c0935403a9fdc169dead5ecaa777.tar.xz krb5-31124ffb81e8c0935403a9fdc169dead5ecaa777.zip | |
Avoid passing null pointers to memcpy/memcmp
By a strict reading of the C standard, memcpy and memcmp have
undefined behavior if their pointer arguments aren't valid object
pointers, even if the length argument is 0. Compilers are becoming
more aggressive about breaking code with undefined behavior, so we
should try to avoid it when possible.
In a krb5_data object, we frequently use NULL as the data value when
the length is 0. Accordingly, we should avoid copying from or
comparing the data field of a length-0 krb5_data object. Add checks
to our wrapper functions (like data_eq and k5_memdup) and to code
which works with possibly-empty krb5_data objects. In a few places,
use wrapper functions to simplify the code rather than adding checks.
Diffstat (limited to 'src/util/support')
| -rw-r--r-- | src/util/support/json.c | 3 | ||||
| -rw-r--r-- | src/util/support/k5buf.c | 3 |
2 files changed, 4 insertions, 2 deletions
diff --git a/src/util/support/json.c b/src/util/support/json.c index c7f6fdd4a..b2dd1333e 100644 --- a/src/util/support/json.c +++ b/src/util/support/json.c @@ -489,7 +489,8 @@ k5_json_string_create_len(const void *data, size_t len, s = alloc_value(&string_type, len + 1); if (s == NULL) return ENOMEM; - memcpy(s, data, len); + if (len > 0) + memcpy(s, data, len); s[len] = '\0'; *val_out = (k5_json_string)s; return 0; diff --git a/src/util/support/k5buf.c b/src/util/support/k5buf.c index 7d491ce2b..778e68b39 100644 --- a/src/util/support/k5buf.c +++ b/src/util/support/k5buf.c @@ -119,7 +119,8 @@ k5_buf_add_len(struct k5buf *buf, const char *data, size_t len) { if (!ensure_space(buf, len)) return; - memcpy(buf->data + buf->len, data, len); + if (len > 0) + memcpy(buf->data + buf->len, data, len); buf->len += len; buf->data[buf->len] = '\0'; } |
