diff options
author | djm@openbsd.org <djm@openbsd.org> | 2016-05-02 08:49:03 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2016-05-02 20:35:04 +1000 |
commit | 1a31d02b2411c4718de58ce796dbb7b5e14db93e (patch) | |
tree | c6e06a9890e71bc97cd3cdc6ce74919e504c8fd8 /sshbuf-misc.c | |
parent | d2d6bf864e52af8491a60dd507f85b74361f5da3 (diff) |
upstream commit
fix signed/unsigned errors reported by clang-3.7; add
sshbuf_dup_string() to replace a common idiom of strdup(sshbuf_ptr()) with
better safety checking; feedback and ok markus@
Upstream-ID: 71f926d9bb3f1efed51319a6daf37e93d57c8820
Diffstat (limited to 'sshbuf-misc.c')
-rw-r--r-- | sshbuf-misc.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/sshbuf-misc.c b/sshbuf-misc.c index 3da4b80e7..15dcfbc79 100644 --- a/sshbuf-misc.c +++ b/sshbuf-misc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sshbuf-misc.c,v 1.5 2015/10/05 17:11:21 djm Exp $ */ | 1 | /* $OpenBSD: sshbuf-misc.c,v 1.6 2016/05/02 08:49:03 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2011 Damien Miller | 3 | * Copyright (c) 2011 Damien Miller |
4 | * | 4 | * |
@@ -136,3 +136,26 @@ sshbuf_b64tod(struct sshbuf *buf, const char *b64) | |||
136 | return 0; | 136 | return 0; |
137 | } | 137 | } |
138 | 138 | ||
139 | char * | ||
140 | sshbuf_dup_string(struct sshbuf *buf) | ||
141 | { | ||
142 | const u_char *p = NULL, *s = sshbuf_ptr(buf); | ||
143 | size_t l = sshbuf_len(buf); | ||
144 | char *r; | ||
145 | |||
146 | if (s == NULL || l > SIZE_MAX) | ||
147 | return NULL; | ||
148 | /* accept a nul only as the last character in the buffer */ | ||
149 | if (l > 0 && (p = memchr(s, '\0', l)) != NULL) { | ||
150 | if (p != s + l - 1) | ||
151 | return NULL; | ||
152 | l--; /* the nul is put back below */ | ||
153 | } | ||
154 | if ((r = malloc(l + 1)) == NULL) | ||
155 | return NULL; | ||
156 | if (l > 0) | ||
157 | memcpy(r, s, l); | ||
158 | r[l] = '\0'; | ||
159 | return r; | ||
160 | } | ||
161 | |||