diff options
author | djm@openbsd.org <djm@openbsd.org> | 2015-01-12 15:18:07 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-13 19:25:52 +1100 |
commit | a7f49dcb527dd17877fcb8d5c3a9a6f550e0bba5 (patch) | |
tree | 0db86a4b895a3ec9bf168df6d385cc9544184dc6 /sshbuf-getput-basic.c | |
parent | 905fe30fca82f38213763616d0d26eb6790bde33 (diff) |
upstream commit
apparently memcpy(x, NULL, 0) is undefined behaviour
according to C99 (cf. sections 7.21.1 and 7.1.4), so check skip memcpy calls
when length==0; ok markus@
Diffstat (limited to 'sshbuf-getput-basic.c')
-rw-r--r-- | sshbuf-getput-basic.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/sshbuf-getput-basic.c b/sshbuf-getput-basic.c index 682b68d58..06d6cc492 100644 --- a/sshbuf-getput-basic.c +++ b/sshbuf-getput-basic.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshbuf-getput-basic.c,v 1.2 2014/12/04 01:49:59 djm Exp $ */ | 1 | /* $OpenBSD: sshbuf-getput-basic.c,v 1.3 2015/01/12 15:18:07 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2011 Damien Miller | 3 | * Copyright (c) 2011 Damien Miller |
4 | * | 4 | * |
@@ -34,7 +34,7 @@ sshbuf_get(struct sshbuf *buf, void *v, size_t len) | |||
34 | 34 | ||
35 | if ((r = sshbuf_consume(buf, len)) < 0) | 35 | if ((r = sshbuf_consume(buf, len)) < 0) |
36 | return r; | 36 | return r; |
37 | if (v != NULL) | 37 | if (v != NULL && len != 0) |
38 | memcpy(v, p, len); | 38 | memcpy(v, p, len); |
39 | return 0; | 39 | return 0; |
40 | } | 40 | } |
@@ -109,7 +109,8 @@ sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp) | |||
109 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); | 109 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); |
110 | return SSH_ERR_ALLOC_FAIL; | 110 | return SSH_ERR_ALLOC_FAIL; |
111 | } | 111 | } |
112 | memcpy(*valp, val, len); | 112 | if (len != 0) |
113 | memcpy(*valp, val, len); | ||
113 | (*valp)[len] = '\0'; | 114 | (*valp)[len] = '\0'; |
114 | } | 115 | } |
115 | if (lenp != NULL) | 116 | if (lenp != NULL) |
@@ -200,7 +201,8 @@ sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp) | |||
200 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); | 201 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); |
201 | return SSH_ERR_ALLOC_FAIL; | 202 | return SSH_ERR_ALLOC_FAIL; |
202 | } | 203 | } |
203 | memcpy(*valp, p, len); | 204 | if (len != 0) |
205 | memcpy(*valp, p, len); | ||
204 | (*valp)[len] = '\0'; | 206 | (*valp)[len] = '\0'; |
205 | } | 207 | } |
206 | if (lenp != NULL) | 208 | if (lenp != NULL) |
@@ -236,7 +238,8 @@ sshbuf_put(struct sshbuf *buf, const void *v, size_t len) | |||
236 | 238 | ||
237 | if ((r = sshbuf_reserve(buf, len, &p)) < 0) | 239 | if ((r = sshbuf_reserve(buf, len, &p)) < 0) |
238 | return r; | 240 | return r; |
239 | memcpy(p, v, len); | 241 | if (len != 0) |
242 | memcpy(p, v, len); | ||
240 | return 0; | 243 | return 0; |
241 | } | 244 | } |
242 | 245 | ||
@@ -352,7 +355,8 @@ sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) | |||
352 | if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0) | 355 | if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0) |
353 | return r; | 356 | return r; |
354 | POKE_U32(d, len); | 357 | POKE_U32(d, len); |
355 | memcpy(d + 4, v, len); | 358 | if (len != 0) |
359 | memcpy(d + 4, v, len); | ||
356 | return 0; | 360 | return 0; |
357 | } | 361 | } |
358 | 362 | ||
@@ -416,6 +420,7 @@ sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len) | |||
416 | POKE_U32(d, len + prepend); | 420 | POKE_U32(d, len + prepend); |
417 | if (prepend) | 421 | if (prepend) |
418 | d[4] = 0; | 422 | d[4] = 0; |
419 | memcpy(d + 4 + prepend, s, len); | 423 | if (len != 0) |
424 | memcpy(d + 4 + prepend, s, len); | ||
420 | return 0; | 425 | return 0; |
421 | } | 426 | } |