OpenCoverage

sshbuf-misc.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/sshbuf-misc.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: sshbuf-misc.c,v 1.6 2016/05/02 08:49:03 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#include "includes.h"-
19-
20#include <sys/types.h>-
21#include <sys/socket.h>-
22#include <netinet/in.h>-
23#include <errno.h>-
24#include <stdlib.h>-
25#ifdef HAVE_STDINT_H-
26#include <stdint.h>-
27#endif-
28#include <stdio.h>-
29#include <limits.h>-
30#include <string.h>-
31#include <resolv.h>-
32#include <ctype.h>-
33-
34#include "ssherr.h"-
35#define SSHBUF_INTERNAL-
36#include "sshbuf.h"-
37-
38void-
39sshbuf_dump_data(const void *s, size_t len, FILE *f)-
40{-
41 size_t i, j;-
42 const u_char *p = (const u_char *)s;-
43-
44 for (i = 0; i < len; i += 16) {
i < lenDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1
45 fprintf(f, "%.4zu: ", i);-
46 for (j = i; j < i + 16; j++) {
j < i + 16Description
TRUEevaluated 16 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1-16
47 if (j < len)
j < lenDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_sshbuf
4-12
48 fprintf(f, "%02x ", p[j]);
executed 4 times by 1 test: fprintf(f, "%02x ", p[j]);
Executed by:
  • test_sshbuf
4
49 else-
50 fprintf(f, " ");
executed 12 times by 1 test: fprintf(f, " ");
Executed by:
  • test_sshbuf
12
51 }-
52 fprintf(f, " ");-
53 for (j = i; j < i + 16; j++) {
j < i + 16Description
TRUEevaluated 16 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1-16
54 if (j < len) {
j < lenDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 12 times by 1 test
Evaluated by:
  • test_sshbuf
4-12
55 if (isascii(p[j]) && isprint(p[j]))
((( p[j] ) & ~0x7f) == 0)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEnever evaluated
((*__ctype_b_l...int) _ISprint)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
0-4
56 fprintf(f, "%c", p[j]);
executed 3 times by 1 test: fprintf(f, "%c", p[j]);
Executed by:
  • test_sshbuf
3
57 else-
58 fprintf(f, ".");
executed 1 time by 1 test: fprintf(f, ".");
Executed by:
  • test_sshbuf
1
59 }-
60 }
executed 16 times by 1 test: end of block
Executed by:
  • test_sshbuf
16
61 fprintf(f, "\n");-
62 }
executed 1 time by 1 test: end of block
Executed by:
  • test_sshbuf
1
63}
executed 1 time by 1 test: end of block
Executed by:
  • test_sshbuf
1
64-
65void-
66sshbuf_dump(struct sshbuf *buf, FILE *f)-
67{-
68 fprintf(f, "buffer %p len = %zu\n", buf, sshbuf_len(buf));-
69 sshbuf_dump_data(sshbuf_ptr(buf), sshbuf_len(buf), f);-
70}
executed 1 time by 1 test: end of block
Executed by:
  • test_sshbuf
1
71-
72char *-
73sshbuf_dtob16(struct sshbuf *buf)-
74{-
75 size_t i, j, len = sshbuf_len(buf);-
76 const u_char *p = sshbuf_ptr(buf);-
77 char *ret;-
78 const char hex[] = "0123456789abcdef";-
79-
80 if (len == 0)
len == 0Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
0-1
81 return strdup("");
never executed: return (__extension__ (__builtin_constant_p ( "" ) && ((size_t)(const void *)(( "" ) + 1) - (size_t)(const void *)( "" ) == 1) ? (((const char *) ( "" ))[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t __len = strlen ( "" ) + 1; char *__retval = (char *) malloc (__len); if (__retval != ((void *)0)) __retval = (char *) memcpy (__retval, "" , __len); __retval; })) : __strdup ( "" ))) ;
never executed: __retval = (char *) memcpy (__retval, "" , __len);
__retval != ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
((const char *... ))[0] == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( "" )Description
TRUEnever evaluated
FALSEnever evaluated
((size_t)(cons...*)( "" ) == 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
82 if (SIZE_MAX / 2 <= len || (ret = malloc(len * 2 + 1)) == NULL)
(1844674407370...UL) / 2 <= lenDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
(ret = malloc(...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
0-1
83 return NULL;
never executed: return ((void *)0) ;
0
84 for (i = j = 0; i < len; i++) {
i < lenDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1-4
85 ret[j++] = hex[(p[i] >> 4) & 0xf];-
86 ret[j++] = hex[p[i] & 0xf];-
87 }
executed 4 times by 1 test: end of block
Executed by:
  • test_sshbuf
4
88 ret[j] = '\0';-
89 return ret;
executed 1 time by 1 test: return ret;
Executed by:
  • test_sshbuf
1
90}-
91-
92char *-
93sshbuf_dtob64(struct sshbuf *buf)-
94{-
95 size_t len = sshbuf_len(buf), plen;-
96 const u_char *p = sshbuf_ptr(buf);-
97 char *ret;-
98 int r;-
99-
100 if (len == 0)
len == 0Description
TRUEnever evaluated
FALSEevaluated 22 times by 2 tests
Evaluated by:
  • ssh-keygen
  • test_sshbuf
0-22
101 return strdup("");
never executed: return (__extension__ (__builtin_constant_p ( "" ) && ((size_t)(const void *)(( "" ) + 1) - (size_t)(const void *)( "" ) == 1) ? (((const char *) ( "" ))[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t __len = strlen ( "" ) + 1; char *__retval = (char *) malloc (__len); if (__retval != ((void *)0)) __retval = (char *) memcpy (__retval, "" , __len); __retval; })) : __strdup ( "" ))) ;
never executed: __retval = (char *) memcpy (__retval, "" , __len);
__retval != ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
((const char *... ))[0] == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( "" )Description
TRUEnever evaluated
FALSEnever evaluated
((size_t)(cons...*)( "" ) == 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
102 plen = ((len + 2) / 3) * 4 + 1;-
103 if (SIZE_MAX / 2 <= len || (ret = malloc(plen)) == NULL)
(1844674407370...UL) / 2 <= lenDescription
TRUEnever evaluated
FALSEevaluated 22 times by 2 tests
Evaluated by:
  • ssh-keygen
  • test_sshbuf
(ret = malloc(...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 22 times by 2 tests
Evaluated by:
  • ssh-keygen
  • test_sshbuf
0-22
104 return NULL;
never executed: return ((void *)0) ;
0
105 if ((r = b64_ntop(p, len, ret, plen)) == -1) {
(r = __b64_nto...t,plen)) == -1Description
TRUEnever evaluated
FALSEevaluated 22 times by 2 tests
Evaluated by:
  • ssh-keygen
  • test_sshbuf
0-22
106 explicit_bzero(ret, plen);-
107 free(ret);-
108 return NULL;
never executed: return ((void *)0) ;
0
109 }-
110 return ret;
executed 22 times by 2 tests: return ret;
Executed by:
  • ssh-keygen
  • test_sshbuf
22
111}-
112-
113int-
114sshbuf_b64tod(struct sshbuf *buf, const char *b64)-
115{-
116 size_t plen = strlen(b64);-
117 int nlen, r;-
118 u_char *p;-
119-
120 if (plen == 0)
plen == 0Description
TRUEnever evaluated
FALSEevaluated 22498 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshbuf
  • test_sshkey
0-22498
121 return 0;
never executed: return 0;
0
122 if ((p = malloc(plen)) == NULL)
(p = malloc(pl...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 22498 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshbuf
  • test_sshkey
0-22498
123 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
124 if ((nlen = b64_pton(b64, p, plen)) < 0) {
(nlen = __b64_...4,p,plen)) < 0Description
TRUEevaluated 428 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 22070 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshbuf
  • test_sshkey
428-22070
125 explicit_bzero(p, plen);-
126 free(p);-
127 return SSH_ERR_INVALID_FORMAT;
executed 428 times by 1 test: return -4;
Executed by:
  • test_sshkey
428
128 }-
129 if ((r = sshbuf_put(buf, p, nlen)) < 0) {
(r = sshbuf_pu... p, nlen)) < 0Description
TRUEnever evaluated
FALSEevaluated 22070 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshbuf
  • test_sshkey
0-22070
130 explicit_bzero(p, plen);-
131 free(p);-
132 return r;
never executed: return r;
0
133 }-
134 explicit_bzero(p, plen);-
135 free(p);-
136 return 0;
executed 22070 times by 5 tests: return 0;
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshbuf
  • test_sshkey
22070
137}-
138-
139char *-
140sshbuf_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)
s == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
l > (18446744073709551615UL)Description
TRUEnever evaluated
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
0-6
147 return NULL;
never executed: return ((void *)0) ;
0
148 /* accept a nul only as the last character in the buffer */-
149 if (l > 0 && (p = memchr(s, '\0', l)) != NULL) {
l > 0Description
TRUEevaluated 5 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
(p = memchr(s,...!= ((void *)0)Description
TRUEevaluated 4 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1-5
150 if (p != s + l - 1)
p != s + l - 1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 3 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
1-3
151 return NULL;
executed 1 time by 1 test: return ((void *)0) ;
Executed by:
  • test_sshbuf
1
152 l--; /* the nul is put back below */-
153 }
executed 3 times by 2 tests: end of block
Executed by:
  • sshd
  • test_sshbuf
3
154 if ((r = malloc(l + 1)) == NULL)
(r = malloc(l ...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 5 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
0-5
155 return NULL;
never executed: return ((void *)0) ;
0
156 if (l > 0)
l > 0Description
TRUEevaluated 4 times by 2 tests
Evaluated by:
  • sshd
  • test_sshbuf
FALSEevaluated 1 time by 1 test
Evaluated by:
  • test_sshbuf
1-4
157 memcpy(r, s, l);
executed 4 times by 2 tests: memcpy(r, s, l);
Executed by:
  • sshd
  • test_sshbuf
4
158 r[l] = '\0';-
159 return r;
executed 5 times by 2 tests: return r;
Executed by:
  • sshd
  • test_sshbuf
5
160}-
161-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2