| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/sshbuf-getput-basic.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: sshbuf-getput-basic.c,v 1.7 2017/06/01 04:51:58 djm Exp $ */ | - | ||||||||||||
| 2 | /* | - | ||||||||||||
| 3 | * Copyright (c) 2011 Damien Miller | - | ||||||||||||
| 4 | * | - | ||||||||||||
| 5 | * Permission to use, copy, modify, and distribute this software for any | - | ||||||||||||
| 6 | * purpose with or without fee is hereby granted, provided that the above | - | ||||||||||||
| 7 | * copyright notice and this permission notice appear in all copies. | - | ||||||||||||
| 8 | * | - | ||||||||||||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | - | ||||||||||||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | - | ||||||||||||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | - | ||||||||||||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | - | ||||||||||||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | - | ||||||||||||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | - | ||||||||||||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | - | ||||||||||||
| 16 | */ | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | #define SSHBUF_INTERNAL | - | ||||||||||||
| 19 | #include "includes.h" | - | ||||||||||||
| 20 | - | |||||||||||||
| 21 | #include <sys/types.h> | - | ||||||||||||
| 22 | - | |||||||||||||
| 23 | #include <stdarg.h> | - | ||||||||||||
| 24 | #include <stdlib.h> | - | ||||||||||||
| 25 | #include <stdio.h> | - | ||||||||||||
| 26 | #include <string.h> | - | ||||||||||||
| 27 | - | |||||||||||||
| 28 | #include "ssherr.h" | - | ||||||||||||
| 29 | #include "sshbuf.h" | - | ||||||||||||
| 30 | - | |||||||||||||
| 31 | int | - | ||||||||||||
| 32 | sshbuf_get(struct sshbuf *buf, void *v, size_t len) | - | ||||||||||||
| 33 | { | - | ||||||||||||
| 34 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 35 | int r; | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | if ((r = sshbuf_consume(buf, len)) < 0)
| 1-65 | ||||||||||||
| 38 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 39 | if (v != NULL && len != 0)
| 0-65 | ||||||||||||
| 40 | memcpy(v, p, len); executed 33 times by 2 tests: memcpy(v, p, len);Executed by:
| 33 | ||||||||||||
| 41 | return 0; executed 65 times by 2 tests: return 0;Executed by:
| 65 | ||||||||||||
| 42 | } | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | int | - | ||||||||||||
| 45 | sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp) | - | ||||||||||||
| 46 | { | - | ||||||||||||
| 47 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 48 | int r; | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | if ((r = sshbuf_consume(buf, 8)) < 0)
| 97-1285256 | ||||||||||||
| 51 | return r; executed 97 times by 2 tests: return r;Executed by:
| 97 | ||||||||||||
| 52 | if (valp != NULL)
| 0-1285256 | ||||||||||||
| 53 | *valp = PEEK_U64(p); executed 1285256 times by 3 tests: *valp = (((u_int64_t)(((const u_char *)(p))[0]) << 56) | ((u_int64_t)(((const u_char *)(p))[1]) << 48) | ((u_int64_t)(((const u_char *)(p))[2]) << 40) | ((u_int64_t)(((const u_char *)(p))[3]) << 32) | ((u_int64_t)(((const u_char *)(p))[4]) << 24) | ((u_int64_t)(((const u_char *)(p))[5]) << 16) | ((u_int64_t)(((const u_char *)(p))[6]) << 8) | (u_int64_t)(((const u_char *)(p))[7]));Executed by:
| 1285256 | ||||||||||||
| 54 | return 0; executed 1285256 times by 3 tests: return 0;Executed by:
| 1285256 | ||||||||||||
| 55 | } | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | int | - | ||||||||||||
| 58 | sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp) | - | ||||||||||||
| 59 | { | - | ||||||||||||
| 60 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 61 | int r; | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | if ((r = sshbuf_consume(buf, 4)) < 0)
| 18-1329270 | ||||||||||||
| 64 | return r; executed 18 times by 2 tests: return r;Executed by:
| 18 | ||||||||||||
| 65 | if (valp != NULL)
| 320-1328950 | ||||||||||||
| 66 | *valp = PEEK_U32(p); executed 1328950 times by 5 tests: *valp = (((u_int32_t)(((const u_char *)(p))[0]) << 24) | ((u_int32_t)(((const u_char *)(p))[1]) << 16) | ((u_int32_t)(((const u_char *)(p))[2]) << 8) | (u_int32_t)(((const u_char *)(p))[3]));Executed by:
| 1328950 | ||||||||||||
| 67 | return 0; executed 1329270 times by 5 tests: return 0;Executed by:
| 1329270 | ||||||||||||
| 68 | } | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | int | - | ||||||||||||
| 71 | sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp) | - | ||||||||||||
| 72 | { | - | ||||||||||||
| 73 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 74 | int r; | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | if ((r = sshbuf_consume(buf, 2)) < 0)
| 1-1239381 | ||||||||||||
| 77 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 78 | if (valp != NULL)
| 0-1239381 | ||||||||||||
| 79 | *valp = PEEK_U16(p); executed 1239381 times by 1 test: *valp = (((u_int16_t)(((const u_char *)(p))[0]) << 8) | (u_int16_t)(((const u_char *)(p))[1]));Executed by:
| 1239381 | ||||||||||||
| 80 | return 0; executed 1239381 times by 1 test: return 0;Executed by:
| 1239381 | ||||||||||||
| 81 | } | - | ||||||||||||
| 82 | - | |||||||||||||
| 83 | int | - | ||||||||||||
| 84 | sshbuf_get_u8(struct sshbuf *buf, u_char *valp) | - | ||||||||||||
| 85 | { | - | ||||||||||||
| 86 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 87 | int r; | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | if ((r = sshbuf_consume(buf, 1)) < 0)
| 1-1275242 | ||||||||||||
| 90 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 91 | if (valp != NULL)
| 5440-1269802 | ||||||||||||
| 92 | *valp = (u_int8_t)*p; executed 1269802 times by 5 tests: *valp = (u_int8_t)*p;Executed by:
| 1269802 | ||||||||||||
| 93 | return 0; executed 1275242 times by 5 tests: return 0;Executed by:
| 1275242 | ||||||||||||
| 94 | } | - | ||||||||||||
| 95 | - | |||||||||||||
| 96 | int | - | ||||||||||||
| 97 | sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp) | - | ||||||||||||
| 98 | { | - | ||||||||||||
| 99 | const u_char *val; | - | ||||||||||||
| 100 | size_t len; | - | ||||||||||||
| 101 | int r; | - | ||||||||||||
| 102 | - | |||||||||||||
| 103 | if (valp != NULL)
| 3200-1357619 | ||||||||||||
| 104 | *valp = NULL; executed 1357619 times by 6 tests: *valp = ((void *)0) ;Executed by:
| 1357619 | ||||||||||||
| 105 | if (lenp != NULL)
| 3200-1357619 | ||||||||||||
| 106 | *lenp = 0; executed 1357619 times by 6 tests: *lenp = 0;Executed by:
| 1357619 | ||||||||||||
| 107 | if ((r = sshbuf_get_string_direct(buf, &val, &len)) < 0)
| 4725-1356094 | ||||||||||||
| 108 | return r; executed 4725 times by 2 tests: return r;Executed by:
| 4725 | ||||||||||||
| 109 | if (valp != NULL) {
| 3200-1352894 | ||||||||||||
| 110 | if ((*valp = malloc(len + 1)) == NULL) {
| 0-1352894 | ||||||||||||
| 111 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); | - | ||||||||||||
| 112 | return SSH_ERR_ALLOC_FAIL; never executed: return -2; | 0 | ||||||||||||
| 113 | } | - | ||||||||||||
| 114 | if (len != 0)
| 74-1352820 | ||||||||||||
| 115 | memcpy(*valp, val, len); executed 1352820 times by 6 tests: memcpy(*valp, val, len);Executed by:
| 1352820 | ||||||||||||
| 116 | (*valp)[len] = '\0'; | - | ||||||||||||
| 117 | } executed 1352894 times by 6 tests: end of blockExecuted by:
| 1352894 | ||||||||||||
| 118 | if (lenp != NULL)
| 3200-1352894 | ||||||||||||
| 119 | *lenp = len; executed 1352894 times by 6 tests: *lenp = len;Executed by:
| 1352894 | ||||||||||||
| 120 | return 0; executed 1356094 times by 6 tests: return 0;Executed by:
| 1356094 | ||||||||||||
| 121 | } | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | int | - | ||||||||||||
| 124 | sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, size_t *lenp) | - | ||||||||||||
| 125 | { | - | ||||||||||||
| 126 | size_t len; | - | ||||||||||||
| 127 | const u_char *p; | - | ||||||||||||
| 128 | int r; | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | if (valp != NULL)
| 319661-1378046 | ||||||||||||
| 131 | *valp = NULL; executed 1378046 times by 6 tests: *valp = ((void *)0) ;Executed by:
| 1378046 | ||||||||||||
| 132 | if (lenp != NULL)
| 319661-1378046 | ||||||||||||
| 133 | *lenp = 0; executed 1378046 times by 6 tests: *lenp = 0;Executed by:
| 1378046 | ||||||||||||
| 134 | if ((r = sshbuf_peek_string_direct(buf, &p, &len)) < 0)
| 6304-1691403 | ||||||||||||
| 135 | return r; executed 6304 times by 2 tests: return r;Executed by:
| 6304 | ||||||||||||
| 136 | if (valp != NULL)
| 318927-1372476 | ||||||||||||
| 137 | *valp = p; executed 1372476 times by 6 tests: *valp = p;Executed by:
| 1372476 | ||||||||||||
| 138 | if (lenp != NULL)
| 318927-1372476 | ||||||||||||
| 139 | *lenp = len; executed 1372476 times by 6 tests: *lenp = len;Executed by:
| 1372476 | ||||||||||||
| 140 | if (sshbuf_consume(buf, len + 4) != 0) {
| 0-1691403 | ||||||||||||
| 141 | /* Shouldn't happen */ | - | ||||||||||||
| 142 | SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR")); | - | ||||||||||||
| 143 | SSHBUF_ABORT(); | - | ||||||||||||
| 144 | return SSH_ERR_INTERNAL_ERROR; never executed: return -1; | 0 | ||||||||||||
| 145 | } | - | ||||||||||||
| 146 | return 0; executed 1691403 times by 6 tests: return 0;Executed by:
| 1691403 | ||||||||||||
| 147 | } | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | int | - | ||||||||||||
| 150 | sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp, | - | ||||||||||||
| 151 | size_t *lenp) | - | ||||||||||||
| 152 | { | - | ||||||||||||
| 153 | u_int32_t len; | - | ||||||||||||
| 154 | const u_char *p = sshbuf_ptr(buf); | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | if (valp != NULL)
| 64-4617117 | ||||||||||||
| 157 | *valp = NULL; executed 4617117 times by 6 tests: *valp = ((void *)0) ;Executed by:
| 4617117 | ||||||||||||
| 158 | if (lenp != NULL)
| 64-4617117 | ||||||||||||
| 159 | *lenp = 0; executed 4617117 times by 6 tests: *lenp = 0;Executed by:
| 4617117 | ||||||||||||
| 160 | if (sshbuf_len(buf) < 4) {
| 343-4616838 | ||||||||||||
| 161 | SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE")); | - | ||||||||||||
| 162 | return SSH_ERR_MESSAGE_INCOMPLETE; executed 343 times by 1 test: return -3;Executed by:
| 343 | ||||||||||||
| 163 | } | - | ||||||||||||
| 164 | len = PEEK_U32(p); | - | ||||||||||||
| 165 | if (len > SSHBUF_SIZE_MAX - 4) {
| 2130983-2485855 | ||||||||||||
| 166 | SSHBUF_DBG(("SSH_ERR_STRING_TOO_LARGE")); | - | ||||||||||||
| 167 | return SSH_ERR_STRING_TOO_LARGE; executed 2485855 times by 2 tests: return -6;Executed by:
| 2485855 | ||||||||||||
| 168 | } | - | ||||||||||||
| 169 | if (sshbuf_len(buf) - 4 < len) {
| 15093-2115890 | ||||||||||||
| 170 | SSHBUF_DBG(("SSH_ERR_MESSAGE_INCOMPLETE")); | - | ||||||||||||
| 171 | return SSH_ERR_MESSAGE_INCOMPLETE; executed 15093 times by 2 tests: return -3;Executed by:
| 15093 | ||||||||||||
| 172 | } | - | ||||||||||||
| 173 | if (valp != NULL)
| 64-2115826 | ||||||||||||
| 174 | *valp = p + 4; executed 2115826 times by 6 tests: *valp = p + 4;Executed by:
| 2115826 | ||||||||||||
| 175 | if (lenp != NULL)
| 64-2115826 | ||||||||||||
| 176 | *lenp = len; executed 2115826 times by 6 tests: *lenp = len;Executed by:
| 2115826 | ||||||||||||
| 177 | return 0; executed 2115890 times by 6 tests: return 0;Executed by:
| 2115890 | ||||||||||||
| 178 | } | - | ||||||||||||
| 179 | - | |||||||||||||
| 180 | int | - | ||||||||||||
| 181 | sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp) | - | ||||||||||||
| 182 | { | - | ||||||||||||
| 183 | size_t len; | - | ||||||||||||
| 184 | const u_char *p, *z; | - | ||||||||||||
| 185 | int r; | - | ||||||||||||
| 186 | - | |||||||||||||
| 187 | if (valp != NULL)
| 0-274625 | ||||||||||||
| 188 | *valp = NULL; executed 274625 times by 6 tests: *valp = ((void *)0) ;Executed by:
| 274625 | ||||||||||||
| 189 | if (lenp != NULL)
| 15534-259091 | ||||||||||||
| 190 | *lenp = 0; executed 15534 times by 2 tests: *lenp = 0;Executed by:
| 15534 | ||||||||||||
| 191 | if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
| 11053-263572 | ||||||||||||
| 192 | return r; executed 11053 times by 2 tests: return r;Executed by:
| 11053 | ||||||||||||
| 193 | /* Allow a \0 only at the end of the string */ | - | ||||||||||||
| 194 | if (len > 0 &&
| 1495-262077 | ||||||||||||
| 195 | (z = memchr(p , '\0', len)) != NULL && z < p + len - 1) {
| 35-261655 | ||||||||||||
| 196 | SSHBUF_DBG(("SSH_ERR_INVALID_FORMAT")); | - | ||||||||||||
| 197 | return SSH_ERR_INVALID_FORMAT; executed 387 times by 3 tests: return -4;Executed by:
| 387 | ||||||||||||
| 198 | } | - | ||||||||||||
| 199 | if ((r = sshbuf_skip_string(buf)) != 0)
| 0-263185 | ||||||||||||
| 200 | return -1; never executed: return -1; | 0 | ||||||||||||
| 201 | if (valp != NULL) {
| 0-263185 | ||||||||||||
| 202 | if ((*valp = malloc(len + 1)) == NULL) {
| 0-263185 | ||||||||||||
| 203 | SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL")); | - | ||||||||||||
| 204 | return SSH_ERR_ALLOC_FAIL; never executed: return -2; | 0 | ||||||||||||
| 205 | } | - | ||||||||||||
| 206 | if (len != 0)
| 1495-261690 | ||||||||||||
| 207 | memcpy(*valp, p, len); executed 261690 times by 6 tests: memcpy(*valp, p, len);Executed by:
| 261690 | ||||||||||||
| 208 | (*valp)[len] = '\0'; | - | ||||||||||||
| 209 | } executed 263185 times by 6 tests: end of blockExecuted by:
| 263185 | ||||||||||||
| 210 | if (lenp != NULL)
| 15350-247835 | ||||||||||||
| 211 | *lenp = (size_t)len; executed 15350 times by 2 tests: *lenp = (size_t)len;Executed by:
| 15350 | ||||||||||||
| 212 | return 0; executed 263185 times by 6 tests: return 0;Executed by:
| 263185 | ||||||||||||
| 213 | } | - | ||||||||||||
| 214 | - | |||||||||||||
| 215 | int | - | ||||||||||||
| 216 | sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v) | - | ||||||||||||
| 217 | { | - | ||||||||||||
| 218 | u_int32_t len; | - | ||||||||||||
| 219 | u_char *p; | - | ||||||||||||
| 220 | int r; | - | ||||||||||||
| 221 | - | |||||||||||||
| 222 | /* | - | ||||||||||||
| 223 | * Use sshbuf_peek_string_direct() to figure out if there is | - | ||||||||||||
| 224 | * a complete string in 'buf' and copy the string directly | - | ||||||||||||
| 225 | * into 'v'. | - | ||||||||||||
| 226 | */ | - | ||||||||||||
| 227 | if ((r = sshbuf_peek_string_direct(buf, NULL, NULL)) != 0 ||
| 0-64 | ||||||||||||
| 228 | (r = sshbuf_get_u32(buf, &len)) != 0 ||
| 0-64 | ||||||||||||
| 229 | (r = sshbuf_reserve(v, len, &p)) != 0 ||
| 0-64 | ||||||||||||
| 230 | (r = sshbuf_get(buf, p, len)) != 0)
| 0-64 | ||||||||||||
| 231 | return r; never executed: return r; | 0 | ||||||||||||
| 232 | return 0; executed 64 times by 1 test: return 0;Executed by:
| 64 | ||||||||||||
| 233 | } | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | int | - | ||||||||||||
| 236 | sshbuf_put(struct sshbuf *buf, const void *v, size_t len) | - | ||||||||||||
| 237 | { | - | ||||||||||||
| 238 | u_char *p; | - | ||||||||||||
| 239 | int r; | - | ||||||||||||
| 240 | - | |||||||||||||
| 241 | if ((r = sshbuf_reserve(buf, len, &p)) < 0)
| 1-1561867 | ||||||||||||
| 242 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 243 | if (len != 0)
| 26501-1535366 | ||||||||||||
| 244 | memcpy(p, v, len); executed 1535366 times by 6 tests: memcpy(p, v, len);Executed by:
| 1535366 | ||||||||||||
| 245 | return 0; executed 1561867 times by 6 tests: return 0;Executed by:
| 1561867 | ||||||||||||
| 246 | } | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | int | - | ||||||||||||
| 249 | sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v) | - | ||||||||||||
| 250 | { | - | ||||||||||||
| 251 | return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v)); executed 42354 times by 3 tests: return sshbuf_put(buf, sshbuf_ptr(v), sshbuf_len(v));Executed by:
| 42354 | ||||||||||||
| 252 | } | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | int | - | ||||||||||||
| 255 | sshbuf_putf(struct sshbuf *buf, const char *fmt, ...) | - | ||||||||||||
| 256 | { | - | ||||||||||||
| 257 | va_list ap; | - | ||||||||||||
| 258 | int r; | - | ||||||||||||
| 259 | - | |||||||||||||
| 260 | va_start(ap, fmt); | - | ||||||||||||
| 261 | r = sshbuf_putfv(buf, fmt, ap); | - | ||||||||||||
| 262 | va_end(ap); | - | ||||||||||||
| 263 | return r; executed 12 times by 2 tests: return r;Executed by:
| 12 | ||||||||||||
| 264 | } | - | ||||||||||||
| 265 | - | |||||||||||||
| 266 | int | - | ||||||||||||
| 267 | sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap) | - | ||||||||||||
| 268 | { | - | ||||||||||||
| 269 | va_list ap2; | - | ||||||||||||
| 270 | int r, len; | - | ||||||||||||
| 271 | u_char *p; | - | ||||||||||||
| 272 | - | |||||||||||||
| 273 | VA_COPY(ap2, ap); | - | ||||||||||||
| 274 | if ((len = vsnprintf(NULL, 0, fmt, ap2)) < 0) {
| 0-12 | ||||||||||||
| 275 | r = SSH_ERR_INVALID_ARGUMENT; | - | ||||||||||||
| 276 | goto out; never executed: goto out; | 0 | ||||||||||||
| 277 | } | - | ||||||||||||
| 278 | if (len == 0) {
| 0-12 | ||||||||||||
| 279 | r = 0; | - | ||||||||||||
| 280 | goto out; /* Nothing to do */ never executed: goto out; | 0 | ||||||||||||
| 281 | } | - | ||||||||||||
| 282 | va_end(ap2); | - | ||||||||||||
| 283 | VA_COPY(ap2, ap); | - | ||||||||||||
| 284 | if ((r = sshbuf_reserve(buf, (size_t)len + 1, &p)) < 0)
| 0-12 | ||||||||||||
| 285 | goto out; never executed: goto out; | 0 | ||||||||||||
| 286 | if ((r = vsnprintf((char *)p, len + 1, fmt, ap2)) != len) {
| 0-12 | ||||||||||||
| 287 | r = SSH_ERR_INTERNAL_ERROR; | - | ||||||||||||
| 288 | goto out; /* Shouldn't happen */ never executed: goto out; | 0 | ||||||||||||
| 289 | } | - | ||||||||||||
| 290 | /* Consume terminating \0 */ | - | ||||||||||||
| 291 | if ((r = sshbuf_consume_end(buf, 1)) != 0)
| 0-12 | ||||||||||||
| 292 | goto out; never executed: goto out; | 0 | ||||||||||||
| 293 | r = 0; | - | ||||||||||||
| 294 | out: code before this statement executed 12 times by 2 tests: out:Executed by:
| 12 | ||||||||||||
| 295 | va_end(ap2); | - | ||||||||||||
| 296 | return r; executed 12 times by 2 tests: return r;Executed by:
| 12 | ||||||||||||
| 297 | } | - | ||||||||||||
| 298 | - | |||||||||||||
| 299 | int | - | ||||||||||||
| 300 | sshbuf_put_u64(struct sshbuf *buf, u_int64_t val) | - | ||||||||||||
| 301 | { | - | ||||||||||||
| 302 | u_char *p; | - | ||||||||||||
| 303 | int r; | - | ||||||||||||
| 304 | - | |||||||||||||
| 305 | if ((r = sshbuf_reserve(buf, 8, &p)) < 0)
| 1-168 | ||||||||||||
| 306 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 307 | POKE_U64(p, val); | - | ||||||||||||
| 308 | return 0; executed 168 times by 3 tests: return 0;Executed by:
| 168 | ||||||||||||
| 309 | } | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | int | - | ||||||||||||
| 312 | sshbuf_put_u32(struct sshbuf *buf, u_int32_t val) | - | ||||||||||||
| 313 | { | - | ||||||||||||
| 314 | u_char *p; | - | ||||||||||||
| 315 | int r; | - | ||||||||||||
| 316 | - | |||||||||||||
| 317 | if ((r = sshbuf_reserve(buf, 4, &p)) < 0)
| 2-1721 | ||||||||||||
| 318 | return r; executed 2 times by 1 test: return r;Executed by:
| 2 | ||||||||||||
| 319 | POKE_U32(p, val); | - | ||||||||||||
| 320 | return 0; executed 1721 times by 4 tests: return 0;Executed by:
| 1721 | ||||||||||||
| 321 | } | - | ||||||||||||
| 322 | - | |||||||||||||
| 323 | int | - | ||||||||||||
| 324 | sshbuf_put_u16(struct sshbuf *buf, u_int16_t val) | - | ||||||||||||
| 325 | { | - | ||||||||||||
| 326 | u_char *p; | - | ||||||||||||
| 327 | int r; | - | ||||||||||||
| 328 | - | |||||||||||||
| 329 | if ((r = sshbuf_reserve(buf, 2, &p)) < 0)
| 1-11 | ||||||||||||
| 330 | return r; executed 1 time by 1 test: return r;Executed by:
| 1 | ||||||||||||
| 331 | POKE_U16(p, val); | - | ||||||||||||
| 332 | return 0; executed 11 times by 1 test: return 0;Executed by:
| 11 | ||||||||||||
| 333 | } | - | ||||||||||||
| 334 | - | |||||||||||||
| 335 | int | - | ||||||||||||
| 336 | sshbuf_put_u8(struct sshbuf *buf, u_char val) | - | ||||||||||||
| 337 | { | - | ||||||||||||
| 338 | u_char *p; | - | ||||||||||||
| 339 | int r; | - | ||||||||||||
| 340 | - | |||||||||||||
| 341 | if ((r = sshbuf_reserve(buf, 1, &p)) < 0)
| 0-8185580 | ||||||||||||
| 342 | return r; never executed: return r; | 0 | ||||||||||||
| 343 | p[0] = val; | - | ||||||||||||
| 344 | return 0; executed 8185580 times by 5 tests: return 0;Executed by:
| 8185580 | ||||||||||||
| 345 | } | - | ||||||||||||
| 346 | - | |||||||||||||
| 347 | int | - | ||||||||||||
| 348 | sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len) | - | ||||||||||||
| 349 | { | - | ||||||||||||
| 350 | u_char *d; | - | ||||||||||||
| 351 | int r; | - | ||||||||||||
| 352 | - | |||||||||||||
| 353 | if (len > SSHBUF_SIZE_MAX - 4) {
| 1-6826 | ||||||||||||
| 354 | SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE")); | - | ||||||||||||
| 355 | return SSH_ERR_NO_BUFFER_SPACE; executed 1 time by 1 test: return -9;Executed by:
| 1 | ||||||||||||
| 356 | } | - | ||||||||||||
| 357 | if ((r = sshbuf_reserve(buf, len + 4, &d)) < 0)
| 3-6823 | ||||||||||||
| 358 | return r; executed 3 times by 1 test: return r;Executed by:
| 3 | ||||||||||||
| 359 | POKE_U32(d, len); | - | ||||||||||||
| 360 | if (len != 0)
| 493-6330 | ||||||||||||
| 361 | memcpy(d + 4, v, len); executed 6330 times by 5 tests: memcpy(d + 4, v, len);Executed by:
| 6330 | ||||||||||||
| 362 | return 0; executed 6823 times by 5 tests: return 0;Executed by:
| 6823 | ||||||||||||
| 363 | } | - | ||||||||||||
| 364 | - | |||||||||||||
| 365 | int | - | ||||||||||||
| 366 | sshbuf_put_cstring(struct sshbuf *buf, const char *v) | - | ||||||||||||
| 367 | { | - | ||||||||||||
| 368 | return sshbuf_put_string(buf, v, v == NULL ? 0 : strlen(v)); executed 3105 times by 5 tests: return sshbuf_put_string(buf, v, v == ((void *)0) ? 0 : strlen(v));Executed by:
| 3105 | ||||||||||||
| 369 | } | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | int | - | ||||||||||||
| 372 | sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v) | - | ||||||||||||
| 373 | { | - | ||||||||||||
| 374 | return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v)); executed 279 times by 4 tests: return sshbuf_put_string(buf, sshbuf_ptr(v), sshbuf_len(v));Executed by:
| 279 | ||||||||||||
| 375 | } | - | ||||||||||||
| 376 | - | |||||||||||||
| 377 | int | - | ||||||||||||
| 378 | sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp) | - | ||||||||||||
| 379 | { | - | ||||||||||||
| 380 | const u_char *p; | - | ||||||||||||
| 381 | size_t len; | - | ||||||||||||
| 382 | struct sshbuf *ret; | - | ||||||||||||
| 383 | int r; | - | ||||||||||||
| 384 | - | |||||||||||||
| 385 | if (buf == NULL || bufp == NULL)
| 0-91140 | ||||||||||||
| 386 | return SSH_ERR_INVALID_ARGUMENT; never executed: return -10; | 0 | ||||||||||||
| 387 | *bufp = NULL; | - | ||||||||||||
| 388 | if ((r = sshbuf_peek_string_direct(buf, &p, &len)) != 0)
| 2157-88983 | ||||||||||||
| 389 | return r; executed 2157 times by 2 tests: return r;Executed by:
| 2157 | ||||||||||||
| 390 | if ((ret = sshbuf_from(p, len)) == NULL)
| 0-88983 | ||||||||||||
| 391 | return SSH_ERR_ALLOC_FAIL; never executed: return -2; | 0 | ||||||||||||
| 392 | if ((r = sshbuf_consume(buf, len + 4)) != 0 || /* Shouldn't happen */
| 0-88983 | ||||||||||||
| 393 | (r = sshbuf_set_parent(ret, buf)) != 0) {
| 0-88983 | ||||||||||||
| 394 | sshbuf_free(ret); | - | ||||||||||||
| 395 | return r; never executed: return r; | 0 | ||||||||||||
| 396 | } | - | ||||||||||||
| 397 | *bufp = ret; | - | ||||||||||||
| 398 | return 0; executed 88983 times by 4 tests: return 0;Executed by:
| 88983 | ||||||||||||
| 399 | } | - | ||||||||||||
| 400 | - | |||||||||||||
| 401 | int | - | ||||||||||||
| 402 | sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len) | - | ||||||||||||
| 403 | { | - | ||||||||||||
| 404 | u_char *d; | - | ||||||||||||
| 405 | const u_char *s = (const u_char *)v; | - | ||||||||||||
| 406 | int r, prepend; | - | ||||||||||||
| 407 | - | |||||||||||||
| 408 | if (len > SSHBUF_SIZE_MAX - 5) {
| 0-46 | ||||||||||||
| 409 | SSHBUF_DBG(("SSH_ERR_NO_BUFFER_SPACE")); | - | ||||||||||||
| 410 | return SSH_ERR_NO_BUFFER_SPACE; never executed: return -9; | 0 | ||||||||||||
| 411 | } | - | ||||||||||||
| 412 | /* Skip leading zero bytes */ | - | ||||||||||||
| 413 | for (; len > 0 && *s == 0; len--, s++)
| 2-50 | ||||||||||||
| 414 | ; executed 6 times by 1 test: ;Executed by:
| 6 | ||||||||||||
| 415 | /* | - | ||||||||||||
| 416 | * If most significant bit is set then prepend a zero byte to | - | ||||||||||||
| 417 | * avoid interpretation as a negative number. | - | ||||||||||||
| 418 | */ | - | ||||||||||||
| 419 | prepend = len > 0 && (s[0] & 0x80) != 0;
| 2-44 | ||||||||||||
| 420 | if ((r = sshbuf_reserve(buf, len + 4 + prepend, &d)) < 0)
| 0-46 | ||||||||||||
| 421 | return r; never executed: return r; | 0 | ||||||||||||
| 422 | POKE_U32(d, len + prepend); | - | ||||||||||||
| 423 | if (prepend)
| 22-24 | ||||||||||||
| 424 | d[4] = 0; executed 22 times by 2 tests: d[4] = 0;Executed by:
| 22 | ||||||||||||
| 425 | if (len != 0)
| 2-44 | ||||||||||||
| 426 | memcpy(d + 4 + prepend, s, len); executed 44 times by 2 tests: memcpy(d + 4 + prepend, s, len);Executed by:
| 44 | ||||||||||||
| 427 | return 0; executed 46 times by 2 tests: return 0;Executed by:
| 46 | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | int | - | ||||||||||||
| 431 | sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf, | - | ||||||||||||
| 432 | const u_char **valp, size_t *lenp) | - | ||||||||||||
| 433 | { | - | ||||||||||||
| 434 | const u_char *d; | - | ||||||||||||
| 435 | size_t len, olen; | - | ||||||||||||
| 436 | int r; | - | ||||||||||||
| 437 | - | |||||||||||||
| 438 | if ((r = sshbuf_peek_string_direct(buf, &d, &olen)) < 0)
| 65080-1242165 | ||||||||||||
| 439 | return r; executed 1242165 times by 2 tests: return r;Executed by:
| 1242165 | ||||||||||||
| 440 | len = olen; | - | ||||||||||||
| 441 | /* Refuse negative (MSB set) bignums */ | - | ||||||||||||
| 442 | if ((len != 0 && (*d & 0x80) != 0))
| 2-65078 | ||||||||||||
| 443 | return SSH_ERR_BIGNUM_IS_NEGATIVE; executed 295 times by 2 tests: return -5;Executed by:
| 295 | ||||||||||||
| 444 | /* Refuse overlong bignums, allow prepended \0 to avoid MSB set */ | - | ||||||||||||
| 445 | if (len > SSHBUF_MAX_BIGNUM + 1 ||
| 1-64784 | ||||||||||||
| 446 | (len == SSHBUF_MAX_BIGNUM + 1 && *d != 0))
| 0-64784 | ||||||||||||
| 447 | return SSH_ERR_BIGNUM_TOO_LARGE; executed 1 time by 1 test: return -7;Executed by:
| 1 | ||||||||||||
| 448 | /* Trim leading zeros */ | - | ||||||||||||
| 449 | while (len > 0 && *d == 0x00) {
| 12-117796 | ||||||||||||
| 450 | d++; | - | ||||||||||||
| 451 | len--; | - | ||||||||||||
| 452 | } executed 53024 times by 6 tests: end of blockExecuted by:
| 53024 | ||||||||||||
| 453 | if (valp != NULL)
| 0-64784 | ||||||||||||
| 454 | *valp = d; executed 64784 times by 6 tests: *valp = d;Executed by:
| 64784 | ||||||||||||
| 455 | if (lenp != NULL)
| 0-64784 | ||||||||||||
| 456 | *lenp = len; executed 64784 times by 6 tests: *lenp = len;Executed by:
| 64784 | ||||||||||||
| 457 | if (sshbuf_consume(buf, olen + 4) != 0) {
| 0-64784 | ||||||||||||
| 458 | /* Shouldn't happen */ | - | ||||||||||||
| 459 | SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR")); | - | ||||||||||||
| 460 | SSHBUF_ABORT(); | - | ||||||||||||
| 461 | return SSH_ERR_INTERNAL_ERROR; never executed: return -1; | 0 | ||||||||||||
| 462 | } | - | ||||||||||||
| 463 | return 0; executed 64784 times by 6 tests: return 0;Executed by:
| 64784 | ||||||||||||
| 464 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |