OpenCoverage

o_str.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/o_str.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 2003-2017 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 "e_os.h"-
11#include <limits.h>-
12#include <openssl/crypto.h>-
13#include "internal/cryptlib.h"-
14#include "internal/o_str.h"-
15-
16int OPENSSL_memcmp(const void *v1, const void *v2, size_t n)-
17{-
18 const unsigned char *c1 = v1, *c2 = v2;-
19 int ret = 0;-
20-
21 while (n && (ret = *c1 - *c2) == 0)
nDescription
TRUEnever evaluated
FALSEnever evaluated
(ret = *c1 - *c2) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
22 n--, c1++, c2++;
never executed: n--, c1++, c2++;
0
23-
24 return ret;
never executed: return ret;
0
25}-
26-
27char *CRYPTO_strdup(const char *str, const char* file, int line)-
28{-
29 char *ret;-
30-
31 if (str == NULL)
str == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 226510 times by 12 tests
Evaluated by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
0-226510
32 return NULL;
never executed: return ((void *)0) ;
0
33 ret = CRYPTO_malloc(strlen(str) + 1, file, line);-
34 if (ret != NULL)
ret != ((void *)0)Description
TRUEevaluated 226510 times by 12 tests
Evaluated by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
FALSEnever evaluated
0-226510
35 strcpy(ret, str);
executed 226510 times by 12 tests: strcpy(ret, str);
Executed by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
226510
36 return ret;
executed 226510 times by 12 tests: return ret;
Executed by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
226510
37}-
38-
39char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)-
40{-
41 size_t maxlen;-
42 char *ret;-
43-
44 if (str == NULL)
str == ((void *)0)Description
TRUEevaluated 998 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 18080 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
998-18080
45 return NULL;
executed 998 times by 1 test: return ((void *)0) ;
Executed by:
  • libcrypto.so.1.1
998
46-
47 maxlen = OPENSSL_strnlen(str, s);-
48-
49 ret = CRYPTO_malloc(maxlen + 1, file, line);-
50 if (ret) {
retDescription
TRUEevaluated 18080 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-18080
51 memcpy(ret, str, maxlen);-
52 ret[maxlen] = '\0';-
53 }
executed 18080 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
18080
54 return ret;
executed 18080 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
18080
55}-
56-
57void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)-
58{-
59 void *ret;-
60-
61 if (data == NULL || siz >= INT_MAX)
data == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 126466 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
siz >= 0x7fffffffDescription
TRUEnever evaluated
FALSEevaluated 126466 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-126466
62 return NULL;
never executed: return ((void *)0) ;
0
63-
64 ret = CRYPTO_malloc(siz, file, line);-
65 if (ret == NULL) {
ret == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 126466 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-126466
66 CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);-
67 return NULL;
never executed: return ((void *)0) ;
0
68 }-
69 return memcpy(ret, data, siz);
executed 126466 times by 1 test: return memcpy(ret, data, siz);
Executed by:
  • libcrypto.so.1.1
126466
70}-
71-
72size_t OPENSSL_strnlen(const char *str, size_t maxlen)-
73{-
74 const char *p;-
75-
76 for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
executed 13728419 times by 12 tests: ;
Executed by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
maxlen-- != 0Description
TRUEevaluated 16194599 times by 12 tests
Evaluated by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
FALSEevaluated 25614 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
*p != '\0'Description
TRUEevaluated 13728419 times by 12 tests
Evaluated by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
FALSEevaluated 2466180 times by 12 tests
Evaluated by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
25614-16194599
77-
78 return p - str;
executed 2491794 times by 12 tests: return p - str;
Executed by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • rdrand_sanitytest
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
2491794
79}-
80-
81size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)-
82{-
83 size_t l = 0;-
84 for (; size > 1 && *src; size--) {
size > 1Description
TRUEevaluated 27559499 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 9058 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
*srcDescription
TRUEevaluated 24856165 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2703334 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
9058-27559499
85 *dst++ = *src++;-
86 l++;-
87 }
executed 24856165 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
24856165
88 if (size)
sizeDescription
TRUEevaluated 2710473 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1919 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1919-2710473
89 *dst = '\0';
executed 2710473 times by 1 test: *dst = '\0';
Executed by:
  • libcrypto.so.1.1
2710473
90 return l + strlen(src);
executed 2712392 times by 1 test: return l + strlen(src);
Executed by:
  • libcrypto.so.1.1
2712392
91}-
92-
93size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)-
94{-
95 size_t l = 0;-
96 for (; size > 0 && *dst; size--, dst++)
size > 0Description
TRUEevaluated 21486302 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
*dstDescription
TRUEevaluated 19355459 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2130843 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-21486302
97 l++;
executed 19355459 times by 1 test: l++;
Executed by:
  • libcrypto.so.1.1
19355459
98 return l + OPENSSL_strlcpy(dst, src, size);
executed 2130843 times by 1 test: return l + OPENSSL_strlcpy(dst, src, size);
Executed by:
  • libcrypto.so.1.1
2130843
99}-
100-
101int OPENSSL_hexchar2int(unsigned char c)-
102{-
103#ifdef CHARSET_EBCDIC-
104 c = os_toebcdic[c];-
105#endif-
106-
107 switch (c) {-
108 case '0':
executed 155917 times by 2 tests: case '0':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
155917
109 return 0;
executed 155917 times by 2 tests: return 0;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
155917
110 case '1':
executed 75519 times by 2 tests: case '1':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
75519
111 return 1;
executed 75519 times by 2 tests: return 1;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
75519
112 case '2':
executed 70030 times by 2 tests: case '2':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
70030
113 return 2;
executed 70030 times by 2 tests: return 2;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
70030
114 case '3':
executed 66553 times by 2 tests: case '3':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
66553
115 return 3;
executed 66553 times by 2 tests: return 3;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
66553
116 case '4':
executed 75176 times by 2 tests: case '4':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
75176
117 return 4;
executed 75176 times by 2 tests: return 4;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
75176
118 case '5':
executed 68575 times by 2 tests: case '5':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
68575
119 return 5;
executed 68575 times by 2 tests: return 5;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
68575
120 case '6':
executed 67523 times by 2 tests: case '6':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
67523
121 return 6;
executed 67523 times by 2 tests: return 6;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
67523
122 case '7':
executed 66151 times by 2 tests: case '7':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
66151
123 return 7;
executed 66151 times by 2 tests: return 7;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
66151
124 case '8':
executed 63978 times by 2 tests: case '8':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
63978
125 return 8;
executed 63978 times by 2 tests: return 8;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
63978
126 case '9':
executed 65097 times by 2 tests: case '9':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
65097
127 return 9;
executed 65097 times by 2 tests: return 9;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
65097
128 case 'a': case 'A':
executed 61123 times by 2 tests: case 'a':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 5470 times by 2 tests: case 'A':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
5470-61123
129 return 0x0A;
executed 66593 times by 2 tests: return 0x0A;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
66593
130 case 'b': case 'B':
executed 58044 times by 2 tests: case 'b':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 5647 times by 2 tests: case 'B':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
5647-58044
131 return 0x0B;
executed 63691 times by 2 tests: return 0x0B;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
63691
132 case 'c': case 'C':
executed 60633 times by 2 tests: case 'c':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 5335 times by 2 tests: case 'C':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
5335-60633
133 return 0x0C;
executed 65968 times by 2 tests: return 0x0C;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
65968
134 case 'd': case 'D':
executed 58883 times by 2 tests: case 'd':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 5608 times by 2 tests: case 'D':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
5608-58883
135 return 0x0D;
executed 64491 times by 2 tests: return 0x0D;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
64491
136 case 'e': case 'E':
executed 59695 times by 2 tests: case 'e':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 5717 times by 2 tests: case 'E':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
5717-59695
137 return 0x0E;
executed 65412 times by 2 tests: return 0x0E;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
65412
138 case 'f': case 'F':
executed 68533 times by 2 tests: case 'f':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
executed 9686 times by 2 tests: case 'F':
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
9686-68533
139 return 0x0F;
executed 78219 times by 2 tests: return 0x0F;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
78219
140 }-
141 return -1;
never executed: return -1;
0
142}-
143-
144/*-
145 * Give a string of hex digits convert to a buffer-
146 */-
147unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)-
148{-
149 unsigned char *hexbuf, *q;-
150 unsigned char ch, cl;-
151 int chi, cli;-
152 const unsigned char *p;-
153 size_t s;-
154-
155 s = strlen(str);-
156 if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
(hexbuf = CRYP...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 3305 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-3305
157 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);-
158 return NULL;
never executed: return ((void *)0) ;
0
159 }-
160 for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
*pDescription
TRUEevaluated 185590 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
FALSEevaluated 3305 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
3305-185590
161 ch = *p++;-
162 if (ch == ':')
ch == ':'Description
TRUEnever evaluated
FALSEevaluated 185590 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-185590
163 continue;
never executed: continue;
0
164 cl = *p++;-
165 if (!cl) {
!clDescription
TRUEnever evaluated
FALSEevaluated 185590 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-185590
166 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,-
167 CRYPTO_R_ODD_NUMBER_OF_DIGITS);-
168 OPENSSL_free(hexbuf);-
169 return NULL;
never executed: return ((void *)0) ;
0
170 }-
171 cli = OPENSSL_hexchar2int(cl);-
172 chi = OPENSSL_hexchar2int(ch);-
173 if (cli < 0 || chi < 0) {
cli < 0Description
TRUEnever evaluated
FALSEevaluated 185590 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
chi < 0Description
TRUEnever evaluated
FALSEevaluated 185590 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
0-185590
174 OPENSSL_free(hexbuf);-
175 CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);-
176 return NULL;
never executed: return ((void *)0) ;
0
177 }-
178 *q++ = (unsigned char)((chi << 4) | cli);-
179 }
executed 185590 times by 2 tests: end of block
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
185590
180-
181 if (len)
lenDescription
TRUEevaluated 3279 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 26 times by 2 tests
Evaluated by:
  • libcrypto.so.1.1
  • sm2_internal_test
26-3279
182 *len = q - hexbuf;
executed 3279 times by 1 test: *len = q - hexbuf;
Executed by:
  • libcrypto.so.1.1
3279
183 return hexbuf;
executed 3305 times by 2 tests: return hexbuf;
Executed by:
  • libcrypto.so.1.1
  • sm2_internal_test
3305
184}-
185-
186/*-
187 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its-
188 * hex representation @@@ (Contents of buffer are always kept in ASCII, also-
189 * on EBCDIC machines)-
190 */-
191char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)-
192{-
193 static const char hexdig[] = "0123456789ABCDEF";-
194 char *tmp, *q;-
195 const unsigned char *p;-
196 int i;-
197-
198 if (len == 0)
len == 0Description
TRUEevaluated 438 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1212 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
438-1212
199 {-
200 return OPENSSL_zalloc(1);
executed 438 times by 1 test: return CRYPTO_zalloc(1, __FILE__, 200);
Executed by:
  • libcrypto.so.1.1
438
201 }-
202-
203 if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {
(tmp = CRYPTO_...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1212 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1212
204 CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);-
205 return NULL;
never executed: return ((void *)0) ;
0
206 }-
207 q = tmp;-
208 for (i = 0, p = buffer; i < len; i++, p++) {
i < lenDescription
TRUEevaluated 134911 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1212 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1212-134911
209 *q++ = hexdig[(*p >> 4) & 0xf];-
210 *q++ = hexdig[*p & 0xf];-
211 *q++ = ':';-
212 }
executed 134911 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
134911
213 q[-1] = 0;-
214#ifdef CHARSET_EBCDIC-
215 ebcdic2ascii(tmp, tmp, q - tmp - 1);-
216#endif-
217-
218 return tmp;
executed 1212 times by 1 test: return tmp;
Executed by:
  • libcrypto.so.1.1
1212
219}-
220-
221int openssl_strerror_r(int errnum, char *buf, size_t buflen)-
222{-
223#if defined(_MSC_VER) && _MSC_VER>=1400-
224 return !strerror_s(buf, buflen, errnum);-
225#elif defined(_GNU_SOURCE)-
226 return strerror_r(errnum, buf, buflen) != NULL;-
227#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \-
228 (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)-
229 /*-
230 * We can use "real" strerror_r. The OpenSSL version differs in that it-
231 * gives 1 on success and 0 on failure for consistency with other OpenSSL-
232 * functions. Real strerror_r does it the other way around-
233 */-
234 return !strerror_r(errnum, buf, buflen);
executed 263398 times by 11 tests: return !strerror_r(errnum, buf, buflen);
Executed by:
  • asn1_internal_test
  • chacha_internal_test
  • ctype_internal_test
  • curve448_internal_test
  • libcrypto.so.1.1
  • modes_internal_test
  • poly1305_internal_test
  • siphash_internal_test
  • sm2_internal_test
  • sm4_internal_test
  • x509_internal_test
263398
235#else-
236 char *err;-
237 /* Fall back to non-thread safe strerror()...its all we can do */-
238 if (buflen < 2)-
239 return 0;-
240 err = strerror(errnum);-
241 /* Can this ever happen? */-
242 if (err == NULL)-
243 return 0;-
244 strncpy(buf, err, buflen - 1);-
245 buf[buflen - 1] = '\0';-
246 return 1;-
247#endif-
248}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2