comparison libtomcrypt/src/misc/crypt/crypt_constants.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents e9dba7abd939
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
109 _C_STRINGIFY(LTC_MILLER_RABIN_REPS), 109 _C_STRINGIFY(LTC_MILLER_RABIN_REPS),
110 #endif 110 #endif
111 111
112 #ifdef LTC_DER 112 #ifdef LTC_DER
113 /* DER handling */ 113 /* DER handling */
114 {"LTC_DER", 1},
114 _C_STRINGIFY(LTC_ASN1_EOL), 115 _C_STRINGIFY(LTC_ASN1_EOL),
115 _C_STRINGIFY(LTC_ASN1_BOOLEAN), 116 _C_STRINGIFY(LTC_ASN1_BOOLEAN),
116 _C_STRINGIFY(LTC_ASN1_INTEGER), 117 _C_STRINGIFY(LTC_ASN1_INTEGER),
117 _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER), 118 _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
118 _C_STRINGIFY(LTC_ASN1_BIT_STRING), 119 _C_STRINGIFY(LTC_ASN1_BIT_STRING),
130 _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING), 131 _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
131 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING), 132 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
132 _C_STRINGIFY(LTC_ASN1_CONSTRUCTED), 133 _C_STRINGIFY(LTC_ASN1_CONSTRUCTED),
133 _C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC), 134 _C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC),
134 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME), 135 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
136 _C_STRINGIFY(LTC_DER_MAX_RECURSION),
137 #else
138 {"LTC_DER", 0},
135 #endif 139 #endif
136 140
137 #ifdef LTC_CTR_MODE 141 #ifdef LTC_CTR_MODE
138 {"LTC_CTR_MODE", 1}, 142 {"LTC_CTR_MODE", 1},
139 _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN), 143 _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
246 * a -1 return value signifies insufficient space made available 250 * a -1 return value signifies insufficient space made available
247 */ 251 */
248 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) { 252 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
249 int i; 253 int i;
250 unsigned int total_len = 0; 254 unsigned int total_len = 0;
251 char number[32], *ptr; 255 char *ptr;
252 int number_len; 256 int number_len;
253 int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]); 257 int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
254 258
255 /* calculate amount of memory required for the list */ 259 /* calculate amount of memory required for the list */
256 for (i=0; i<count; i++) { 260 for (i=0; i<count; i++) {
257 total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1; 261 number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
258 /* the above +1 is for the commas */ 262 if (number_len < 0)
259 number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
260 if ((number_len < 0) ||
261 ((unsigned int)number_len >= sizeof(number)))
262 return -1; 263 return -1;
263 total_len += number_len + 1; 264 total_len += number_len;
264 /* this last +1 is for newlines (and ending NULL) */
265 } 265 }
266 266
267 if (names_list == NULL) { 267 if (names_list == NULL) {
268 *names_list_size = total_len; 268 *names_list_size = total_len;
269 } else { 269 } else {
271 return -1; 271 return -1;
272 } 272 }
273 /* build the names list */ 273 /* build the names list */
274 ptr = names_list; 274 ptr = names_list;
275 for (i=0; i<count; i++) { 275 for (i=0; i<count; i++) {
276 strcpy(ptr, _crypt_constants[i].name); 276 number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
277 ptr += strlen(_crypt_constants[i].name); 277 if (number_len < 0) return -1;
278 strcpy(ptr, ","); 278 if ((unsigned int)number_len > total_len) return -1;
279 ptr += 1; 279 total_len -= number_len;
280
281 number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
282 strcpy(ptr, number);
283 ptr += number_len; 280 ptr += number_len;
284 strcpy(ptr, "\n");
285 ptr += 1;
286 } 281 }
287 /* to remove the trailing new-line */ 282 /* to remove the trailing new-line */
288 ptr -= 1; 283 ptr -= 1;
289 *ptr = 0; 284 *ptr = 0;
290 } 285 }