summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorGreg Hudson <ghudson@mit.edu>2013-04-08 15:32:31 -0400
committerGreg Hudson <ghudson@mit.edu>2013-04-08 15:32:31 -0400
commit31124ffb81e8c0935403a9fdc169dead5ecaa777 (patch)
tree837d49e7ef8de324f8ad288ab3231ca2acdcdbd7 /src/util
parentcaaf72893a5be61822763eb471f4d573992479ed (diff)
downloadkrb5-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')
-rw-r--r--src/util/support/json.c3
-rw-r--r--src/util/support/k5buf.c3
2 files changed, 4 insertions, 2 deletions
diff --git a/src/util/support/json.c b/src/util/support/json.c
index c7f6fdd4a9..b2dd1333e7 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 7d491ce2bd..778e68b39b 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';
}