OpenCoverage

bss_mem.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bio/bss_mem.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
4 * Licensed under the OpenSSL license (the "License"). You may not use-
5 * this file except in compliance with the License. You can obtain a copy-
6 * in the file LICENSE in the source distribution or at-
7 * https://www.openssl.org/source/license.html-
8 */-
9-
10#include <stdio.h>-
11#include <errno.h>-
12#include "bio_lcl.h"-
13#include "internal/cryptlib.h"-
14-
15static int mem_write(BIO *h, const char *buf, int num);-
16static int mem_read(BIO *h, char *buf, int size);-
17static int mem_puts(BIO *h, const char *str);-
18static int mem_gets(BIO *h, char *str, int size);-
19static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);-
20static int mem_new(BIO *h);-
21static int secmem_new(BIO *h);-
22static int mem_free(BIO *data);-
23static int mem_buf_free(BIO *data, int free_all);-
24static int mem_buf_sync(BIO *h);-
25-
26static const BIO_METHOD mem_method = {-
27 BIO_TYPE_MEM,-
28 "memory buffer",-
29 /* TODO: Convert to new style write function */-
30 bwrite_conv,-
31 mem_write,-
32 /* TODO: Convert to new style read function */-
33 bread_conv,-
34 mem_read,-
35 mem_puts,-
36 mem_gets,-
37 mem_ctrl,-
38 mem_new,-
39 mem_free,-
40 NULL, /* mem_callback_ctrl */-
41};-
42-
43static const BIO_METHOD secmem_method = {-
44 BIO_TYPE_MEM,-
45 "secure memory buffer",-
46 /* TODO: Convert to new style write function */-
47 bwrite_conv,-
48 mem_write,-
49 /* TODO: Convert to new style read function */-
50 bread_conv,-
51 mem_read,-
52 mem_puts,-
53 mem_gets,-
54 mem_ctrl,-
55 secmem_new,-
56 mem_free,-
57 NULL, /* mem_callback_ctrl */-
58};-
59-
60/* BIO memory stores buffer and read pointer */-
61typedef struct bio_buf_mem_st {-
62 struct buf_mem_st *buf; /* allocated buffer */-
63 struct buf_mem_st *readp; /* read pointer */-
64} BIO_BUF_MEM;-
65-
66/*-
67 * bio->num is used to hold the value to return on 'empty', if it is 0,-
68 * should_retry is not set-
69 */-
70-
71const BIO_METHOD *BIO_s_mem(void)-
72{-
73 return &mem_method;
executed 81666 times by 1 test: return &mem_method;
Executed by:
  • libcrypto.so.1.1
81666
74}-
75-
76const BIO_METHOD *BIO_s_secmem(void)-
77{-
78 return(&secmem_method);
executed 7726 times by 1 test: return(&secmem_method);
Executed by:
  • libcrypto.so.1.1
7726
79}-
80-
81BIO *BIO_new_mem_buf(const void *buf, int len)-
82{-
83 BIO *ret;-
84 BUF_MEM *b;-
85 BIO_BUF_MEM *bb;-
86 size_t sz;-
87-
88 if (buf == NULL) {
buf == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 37022 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-37022
89 BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);-
90 return NULL;
never executed: return ((void *)0) ;
0
91 }-
92 sz = (len < 0) ? strlen(buf) : (size_t)len;
(len < 0)Description
TRUEnever evaluated
FALSEevaluated 37022 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-37022
93 if ((ret = BIO_new(BIO_s_mem())) == NULL)
(ret = BIO_new...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 37022 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-37022
94 return NULL;
never executed: return ((void *)0) ;
0
95 bb = (BIO_BUF_MEM *)ret->ptr;-
96 b = bb->buf;-
97 /* Cast away const and trust in the MEM_RDONLY flag. */-
98 b->data = (void *)buf;-
99 b->length = sz;-
100 b->max = sz;-
101 *bb->readp = *bb->buf;-
102 ret->flags |= BIO_FLAGS_MEM_RDONLY;-
103 /* Since this is static data retrying won't help */-
104 ret->num = 0;-
105 return ret;
executed 37022 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
37022
106}-
107-
108static int mem_init(BIO *bi, unsigned long flags)-
109{-
110 BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));-
111-
112 if (bb == NULL)
bb == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-111695
113 return 0;
never executed: return 0;
0
114 if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
(bb->buf = BUF...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-111695
115 OPENSSL_free(bb);-
116 return 0;
never executed: return 0;
0
117 }-
118 if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
(bb->readp = C...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-111695
119 BUF_MEM_free(bb->buf);-
120 OPENSSL_free(bb);-
121 return 0;
never executed: return 0;
0
122 }-
123 *bb->readp = *bb->buf;-
124 bi->shutdown = 1;-
125 bi->init = 1;-
126 bi->num = -1;-
127 bi->ptr = (char *)bb;-
128 return 1;
executed 111695 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
111695
129}-
130-
131static int mem_new(BIO *bi)-
132{-
133 return mem_init(bi, 0L);
executed 96243 times by 1 test: return mem_init(bi, 0L);
Executed by:
  • libcrypto.so.1.1
96243
134}-
135-
136static int secmem_new(BIO *bi)-
137{-
138 return mem_init(bi, BUF_MEM_FLAG_SECURE);
executed 15452 times by 1 test: return mem_init(bi, 0x01);
Executed by:
  • libcrypto.so.1.1
15452
139}-
140-
141static int mem_free(BIO *a)-
142{-
143 return mem_buf_free(a, 1);
executed 111695 times by 1 test: return mem_buf_free(a, 1);
Executed by:
  • libcrypto.so.1.1
111695
144}-
145-
146static int mem_buf_free(BIO *a, int free_all)-
147{-
148 if (a == NULL)
a == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-111695
149 return 0;
never executed: return 0;
0
150-
151 if (a->shutdown && a->init && a->ptr != NULL) {
a->shutdownDescription
TRUEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
a->initDescription
TRUEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
a->ptr != ((void *)0)Description
TRUEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-111695
152 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;-
153 BUF_MEM *b = bb->buf;-
154-
155 if (a->flags & BIO_FLAGS_MEM_RDONLY)
a->flags & 0x200Description
TRUEevaluated 37039 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 74656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
37039-74656
156 b->data = NULL;
executed 37039 times by 1 test: b->data = ((void *)0) ;
Executed by:
  • libcrypto.so.1.1
37039
157 BUF_MEM_free(b);-
158 if (free_all) {
free_allDescription
TRUEevaluated 111695 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-111695
159 OPENSSL_free(bb->readp);-
160 OPENSSL_free(bb);-
161 }
executed 111695 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
111695
162 a->ptr = NULL;-
163 }
executed 111695 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
111695
164 return 1;
executed 111695 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
111695
165}-
166-
167/*-
168 * Reallocate memory buffer if read pointer differs-
169 */-
170static int mem_buf_sync(BIO *b)-
171{-
172 if (b != NULL && b->init != 0 && b->ptr != NULL) {
b != ((void *)0)Description
TRUEevaluated 346472 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
b->init != 0Description
TRUEevaluated 346472 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
b->ptr != ((void *)0)Description
TRUEevaluated 346472 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-346472
173 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;-
174-
175 if (bbm->readp->data != bbm->buf->data) {
bbm->readp->da...bbm->buf->dataDescription
TRUEevaluated 7781 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 338691 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
7781-338691
176 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);-
177 bbm->buf->length = bbm->readp->length;-
178 bbm->readp->data = bbm->buf->data;-
179 }
executed 7781 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
7781
180 }
executed 346472 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
346472
181 return 0;
executed 346472 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
346472
182}-
183-
184static int mem_read(BIO *b, char *out, int outl)-
185{-
186 int ret = -1;-
187 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;-
188 BUF_MEM *bm = bbm->readp;-
189-
190 BIO_clear_retry_flags(b);-
191 ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
outl >= 0Description
TRUEevaluated 929320 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
(size_t)outl > bm->lengthDescription
TRUEevaluated 87666 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 841654 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-929320
192 if ((out != NULL) && (ret > 0)) {
(out != ((void *)0) )Description
TRUEevaluated 929320 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
(ret > 0)Description
TRUEevaluated 862038 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 67282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-929320
193 memcpy(out, bm->data, ret);-
194 bm->length -= ret;-
195 bm->data += ret;-
196 } else if (bm->length == 0) {
executed 862038 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
bm->length == 0Description
TRUEevaluated 67282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-862038
197 ret = b->num;-
198 if (ret != 0)
ret != 0Description
TRUEevaluated 30279 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 37003 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
30279-37003
199 BIO_set_retry_read(b);
executed 30279 times by 1 test: BIO_set_flags(b, (0x01|0x08));
Executed by:
  • libcrypto.so.1.1
30279
200 }
executed 67282 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
67282
201 return ret;
executed 929320 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
929320
202}-
203-
204static int mem_write(BIO *b, const char *in, int inl)-
205{-
206 int ret = -1;-
207 int blen;-
208 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;-
209-
210 if (in == NULL) {
in == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 328234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-328234
211 BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);-
212 goto end;
never executed: goto end;
0
213 }-
214 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
b->flags & 0x200Description
TRUEnever evaluated
FALSEevaluated 328234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-328234
215 BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);-
216 goto end;
never executed: goto end;
0
217 }-
218 BIO_clear_retry_flags(b);-
219 if (inl == 0)
inl == 0Description
TRUEnever evaluated
FALSEevaluated 328234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-328234
220 return 0;
never executed: return 0;
0
221 blen = bbm->readp->length;-
222 mem_buf_sync(b);-
223 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
BUF_MEM_grow_c...en + inl) == 0Description
TRUEnever evaluated
FALSEevaluated 328234 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-328234
224 goto end;
never executed: goto end;
0
225 memcpy(bbm->buf->data + blen, in, inl);-
226 *bbm->readp = *bbm->buf;-
227 ret = inl;-
228 end:
code before this statement executed 328234 times by 1 test: end:
Executed by:
  • libcrypto.so.1.1
328234
229 return ret;
executed 328234 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
328234
230}-
231-
232static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)-
233{-
234 long ret = 1;-
235 char **pptr;-
236 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;-
237 BUF_MEM *bm;-
238-
239 switch (cmd) {-
240 case BIO_CTRL_RESET:
executed 1974 times by 1 test: case 1:
Executed by:
  • libcrypto.so.1.1
1974
241 bm = bbm->buf;-
242 if (bm->data != NULL) {
bm->data != ((void *)0)Description
TRUEevaluated 1968 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6-1968
243 /* For read only case reset to the start again */-
244 if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
(b->flags & 0x200)Description
TRUEnever evaluated
FALSEevaluated 1968 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(b->flags & 0x400)Description
TRUEnever evaluated
FALSEevaluated 1968 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1968
245 bm->length = bm->max;-
246 } else {
never executed: end of block
0
247 memset(bm->data, 0, bm->max);-
248 bm->length = 0;-
249 }
executed 1968 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1968
250 *bbm->readp = *bbm->buf;-
251 }
executed 1968 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1968
252 break;
executed 1974 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
1974
253 case BIO_CTRL_EOF:
never executed: case 2:
0
254 bm = bbm->readp;-
255 ret = (long)(bm->length == 0);-
256 break;
never executed: break;
0
257 case BIO_C_SET_BUF_MEM_EOF_RETURN:
executed 927 times by 1 test: case 130:
Executed by:
  • libcrypto.so.1.1
927
258 b->num = (int)num;-
259 break;
executed 927 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
927
260 case BIO_CTRL_INFO:
executed 23655 times by 1 test: case 3:
Executed by:
  • libcrypto.so.1.1
23655
261 bm = bbm->readp;-
262 ret = (long)bm->length;-
263 if (ptr != NULL) {
ptr != ((void *)0)Description
TRUEevaluated 5424 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 18231 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
5424-18231
264 pptr = (char **)ptr;-
265 *pptr = (char *)&(bm->data[0]);-
266 }
executed 5424 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
5424
267 break;
executed 23655 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
23655
268 case BIO_C_SET_BUF_MEM:
never executed: case 114:
0
269 mem_buf_free(b, 0);-
270 b->shutdown = (int)num;-
271 bbm->buf = ptr;-
272 *bbm->readp = *bbm->buf;-
273 b->ptr = bbm;-
274 break;
never executed: break;
0
275 case BIO_C_GET_BUF_MEM_PTR:
executed 18238 times by 1 test: case 115:
Executed by:
  • libcrypto.so.1.1
18238
276 if (ptr != NULL) {
ptr != ((void *)0)Description
TRUEevaluated 18238 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-18238
277 mem_buf_sync(b);-
278 bm = bbm->readp;-
279 pptr = (char **)ptr;-
280 *pptr = (char *)bm;-
281 }
executed 18238 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
18238
282 break;
executed 18238 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
18238
283 case BIO_CTRL_GET_CLOSE:
never executed: case 8:
0
284 ret = (long)b->shutdown;-
285 break;
never executed: break;
0
286 case BIO_CTRL_SET_CLOSE:
executed 9057 times by 1 test: case 9:
Executed by:
  • libcrypto.so.1.1
9057
287 b->shutdown = (int)num;-
288 break;
executed 9057 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
9057
289 case BIO_CTRL_WPENDING:
executed 483 times by 1 test: case 13:
Executed by:
  • libcrypto.so.1.1
483
290 ret = 0L;-
291 break;
executed 483 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
483
292 case BIO_CTRL_PENDING:
executed 1564 times by 1 test: case 10:
Executed by:
  • libcrypto.so.1.1
1564
293 bm = bbm->readp;-
294 ret = (long)bm->length;-
295 break;
executed 1564 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
1564
296 case BIO_CTRL_DUP:
never executed: case 12:
0
297 case BIO_CTRL_FLUSH:
executed 17065 times by 1 test: case 11:
Executed by:
  • libcrypto.so.1.1
17065
298 ret = 1;-
299 break;
executed 17065 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
17065
300 case BIO_CTRL_PUSH:
executed 45670 times by 1 test: case 6:
Executed by:
  • libcrypto.so.1.1
45670
301 case BIO_CTRL_POP:
executed 8620 times by 1 test: case 7:
Executed by:
  • libcrypto.so.1.1
8620
302 default:
executed 7648 times by 1 test: default:
Executed by:
  • libcrypto.so.1.1
7648
303 ret = 0;-
304 break;
executed 61938 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
61938
305 }-
306 return ret;
executed 134901 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
134901
307}-
308-
309static int mem_gets(BIO *bp, char *buf, int size)-
310{-
311 int i, j;-
312 int ret = -1;-
313 char *p;-
314 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;-
315 BUF_MEM *bm = bbm->readp;-
316-
317 BIO_clear_retry_flags(bp);-
318 j = bm->length;-
319 if ((size - 1) < j)
(size - 1) < jDescription
TRUEevaluated 73714 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 57017 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
57017-73714
320 j = size - 1;
executed 73714 times by 1 test: j = size - 1;
Executed by:
  • libcrypto.so.1.1
73714
321 if (j <= 0) {
j <= 0Description
TRUEevaluated 591 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 130140 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
591-130140
322 *buf = '\0';-
323 return 0;
executed 591 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
591
324 }-
325 p = bm->data;-
326 for (i = 0; i < j; i++) {
i < jDescription
TRUEevaluated 5014074 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 402 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
402-5014074
327 if (p[i] == '\n') {
p[i] == '\n'Description
TRUEevaluated 129738 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4884336 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
129738-4884336
328 i++;-
329 break;
executed 129738 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
129738
330 }-
331 }
executed 4884336 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
4884336
332-
333 /*-
334 * i is now the max num of bytes to copy, either j or up to-
335 * and including the first newline-
336 */-
337-
338 i = mem_read(bp, buf, i);-
339 if (i > 0)
i > 0Description
TRUEevaluated 130140 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-130140
340 buf[i] = '\0';
executed 130140 times by 1 test: buf[i] = '\0';
Executed by:
  • libcrypto.so.1.1
130140
341 ret = i;-
342 return ret;
executed 130140 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
130140
343}-
344-
345static int mem_puts(BIO *bp, const char *str)-
346{-
347 int n, ret;-
348-
349 n = strlen(str);-
350 ret = mem_write(bp, str, n);-
351 /* memory semantics is that it will always work */-
352 return ret;
executed 263329 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
263329
353}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2