Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/buffer/buffer.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 "internal/cryptlib.h" | - | ||||||
12 | #include <openssl/buffer.h> | - | ||||||
13 | - | |||||||
14 | /* | - | ||||||
15 | * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That | - | ||||||
16 | * function is applied in several functions in this file and this limit | - | ||||||
17 | * ensures that the result fits in an int. | - | ||||||
18 | */ | - | ||||||
19 | #define LIMIT_BEFORE_EXPANSION 0x5ffffffc | - | ||||||
20 | - | |||||||
21 | BUF_MEM *BUF_MEM_new_ex(unsigned long flags) | - | ||||||
22 | { | - | ||||||
23 | BUF_MEM *ret; | - | ||||||
24 | - | |||||||
25 | ret = BUF_MEM_new(); | - | ||||||
26 | if (ret != NULL)
| 0-111695 | ||||||
27 | ret->flags = flags; executed 111695 times by 1 test: ret->flags = flags; Executed by:
| 111695 | ||||||
28 | return ret; executed 111695 times by 1 test: return ret; Executed by:
| 111695 | ||||||
29 | } | - | ||||||
30 | - | |||||||
31 | BUF_MEM *BUF_MEM_new(void) | - | ||||||
32 | { | - | ||||||
33 | BUF_MEM *ret; | - | ||||||
34 | - | |||||||
35 | ret = OPENSSL_zalloc(sizeof(*ret)); | - | ||||||
36 | if (ret == NULL) {
| 0-520915 | ||||||
37 | BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE); | - | ||||||
38 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
39 | } | - | ||||||
40 | return ret; executed 520915 times by 1 test: return ret; Executed by:
| 520915 | ||||||
41 | } | - | ||||||
42 | - | |||||||
43 | void BUF_MEM_free(BUF_MEM *a) | - | ||||||
44 | { | - | ||||||
45 | if (a == NULL)
| 48617-400405 | ||||||
46 | return; executed 48617 times by 1 test: return; Executed by:
| 48617 | ||||||
47 | if (a->data != NULL) {
| 185075-215330 | ||||||
48 | if (a->flags & BUF_MEM_FLAG_SECURE)
| 7751-177324 | ||||||
49 | OPENSSL_secure_clear_free(a->data, a->max); executed 7751 times by 1 test: CRYPTO_secure_clear_free(a->data, a->max, __FILE__, 49); Executed by:
| 7751 | ||||||
50 | else | - | ||||||
51 | OPENSSL_clear_free(a->data, a->max); executed 177324 times by 1 test: CRYPTO_clear_free(a->data, a->max, __FILE__, 51); Executed by:
| 177324 | ||||||
52 | } | - | ||||||
53 | OPENSSL_free(a); | - | ||||||
54 | } executed 400405 times by 1 test: end of block Executed by:
| 400405 | ||||||
55 | - | |||||||
56 | /* Allocate a block of secure memory; copy over old data if there | - | ||||||
57 | * was any, and then free it. */ | - | ||||||
58 | static char *sec_alloc_realloc(BUF_MEM *str, size_t len) | - | ||||||
59 | { | - | ||||||
60 | char *ret; | - | ||||||
61 | - | |||||||
62 | ret = OPENSSL_secure_malloc(len); | - | ||||||
63 | if (str->data != NULL) {
| 7751-33012 | ||||||
64 | if (ret != NULL) {
| 0-33012 | ||||||
65 | memcpy(ret, str->data, str->length); | - | ||||||
66 | OPENSSL_secure_clear_free(str->data, str->length); | - | ||||||
67 | str->data = NULL; | - | ||||||
68 | } executed 33012 times by 1 test: end of block Executed by:
| 33012 | ||||||
69 | } executed 33012 times by 1 test: end of block Executed by:
| 33012 | ||||||
70 | return ret; executed 40763 times by 1 test: return ret; Executed by:
| 40763 | ||||||
71 | } | - | ||||||
72 | - | |||||||
73 | size_t BUF_MEM_grow(BUF_MEM *str, size_t len) | - | ||||||
74 | { | - | ||||||
75 | char *ret; | - | ||||||
76 | size_t n; | - | ||||||
77 | - | |||||||
78 | if (str->length >= len) {
| 168621-279011 | ||||||
79 | str->length = len; | - | ||||||
80 | return len; executed 168621 times by 1 test: return len; Executed by:
| 168621 | ||||||
81 | } | - | ||||||
82 | if (str->max >= len) {
| 18361-260650 | ||||||
83 | if (str->data != NULL)
| 0-18361 | ||||||
84 | memset(&str->data[str->length], 0, len - str->length); executed 18361 times by 1 test: memset(&str->data[str->length], 0, len - str->length); Executed by:
| 18361 | ||||||
85 | str->length = len; | - | ||||||
86 | return len; executed 18361 times by 1 test: return len; Executed by:
| 18361 | ||||||
87 | } | - | ||||||
88 | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ | - | ||||||
89 | if (len > LIMIT_BEFORE_EXPANSION) {
| 0-260650 | ||||||
90 | BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE); | - | ||||||
91 | return 0; never executed: return 0; | 0 | ||||||
92 | } | - | ||||||
93 | n = (len + 3) / 3 * 4; | - | ||||||
94 | if ((str->flags & BUF_MEM_FLAG_SECURE))
| 0-260650 | ||||||
95 | ret = sec_alloc_realloc(str, n); never executed: ret = sec_alloc_realloc(str, n); | 0 | ||||||
96 | else | - | ||||||
97 | ret = OPENSSL_realloc(str->data, n); executed 260650 times by 1 test: ret = CRYPTO_realloc(str->data, n, __FILE__, 97); Executed by:
| 260650 | ||||||
98 | if (ret == NULL) {
| 0-260650 | ||||||
99 | BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE); | - | ||||||
100 | len = 0; | - | ||||||
101 | } else { never executed: end of block | 0 | ||||||
102 | str->data = ret; | - | ||||||
103 | str->max = n; | - | ||||||
104 | memset(&str->data[str->length], 0, len - str->length); | - | ||||||
105 | str->length = len; | - | ||||||
106 | } executed 260650 times by 1 test: end of block Executed by:
| 260650 | ||||||
107 | return len; executed 260650 times by 1 test: return len; Executed by:
| 260650 | ||||||
108 | } | - | ||||||
109 | - | |||||||
110 | size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len) | - | ||||||
111 | { | - | ||||||
112 | char *ret; | - | ||||||
113 | size_t n; | - | ||||||
114 | - | |||||||
115 | if (str->length >= len) {
| 25688-1016139 | ||||||
116 | if (str->data != NULL)
| 0-25688 | ||||||
117 | memset(&str->data[len], 0, str->length - len); executed 25688 times by 1 test: memset(&str->data[len], 0, str->length - len); Executed by:
| 25688 | ||||||
118 | str->length = len; | - | ||||||
119 | return len; executed 25688 times by 1 test: return len; Executed by:
| 25688 | ||||||
120 | } | - | ||||||
121 | if (str->max >= len) {
| 230510-785629 | ||||||
122 | memset(&str->data[str->length], 0, len - str->length); | - | ||||||
123 | str->length = len; | - | ||||||
124 | return len; executed 785629 times by 1 test: return len; Executed by:
| 785629 | ||||||
125 | } | - | ||||||
126 | /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ | - | ||||||
127 | if (len > LIMIT_BEFORE_EXPANSION) {
| 0-230510 | ||||||
128 | BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE); | - | ||||||
129 | return 0; never executed: return 0; | 0 | ||||||
130 | } | - | ||||||
131 | n = (len + 3) / 3 * 4; | - | ||||||
132 | if ((str->flags & BUF_MEM_FLAG_SECURE))
| 40763-189747 | ||||||
133 | ret = sec_alloc_realloc(str, n); executed 40763 times by 1 test: ret = sec_alloc_realloc(str, n); Executed by:
| 40763 | ||||||
134 | else | - | ||||||
135 | ret = OPENSSL_clear_realloc(str->data, str->max, n); executed 189747 times by 1 test: ret = CRYPTO_clear_realloc(str->data, str->max, n, __FILE__, 135); Executed by:
| 189747 | ||||||
136 | if (ret == NULL) {
| 0-230510 | ||||||
137 | BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE); | - | ||||||
138 | len = 0; | - | ||||||
139 | } else { never executed: end of block | 0 | ||||||
140 | str->data = ret; | - | ||||||
141 | str->max = n; | - | ||||||
142 | memset(&str->data[str->length], 0, len - str->length); | - | ||||||
143 | str->length = len; | - | ||||||
144 | } executed 230510 times by 1 test: end of block Executed by:
| 230510 | ||||||
145 | return len; executed 230510 times by 1 test: return len; Executed by:
| 230510 | ||||||
146 | } | - | ||||||
147 | - | |||||||
148 | void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size) | - | ||||||
149 | { | - | ||||||
150 | size_t i; | - | ||||||
151 | if (in) {
| 3-123 | ||||||
152 | out += size - 1; | - | ||||||
153 | for (i = 0; i < size; i++)
| 3-24 | ||||||
154 | *out-- = *in++; executed 24 times by 1 test: *out-- = *in++; Executed by:
| 24 | ||||||
155 | } else { executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
156 | unsigned char *q; | - | ||||||
157 | char c; | - | ||||||
158 | q = out + size - 1; | - | ||||||
159 | for (i = 0; i < size / 2; i++) {
| 123-348 | ||||||
160 | c = *q; | - | ||||||
161 | *q-- = *out; | - | ||||||
162 | *out++ = c; | - | ||||||
163 | } executed 348 times by 1 test: end of block Executed by:
| 348 | ||||||
164 | } executed 123 times by 1 test: end of block Executed by:
| 123 | ||||||
165 | } | - | ||||||
Source code | Switch to Preprocessed file |