Rewrite str_replace using snprintf for security and fix clang-tidy (#2350)

This commit is contained in:
Chen 2024-05-08 22:23:23 +08:00 committed by GitHub
parent f81eb3affa
commit 9378216492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

17
cs.c
View File

@ -776,18 +776,23 @@ cs_err CAPSTONE_API cs_close(csh *handle)
return CS_ERR_OK;
}
// replace str1 in target with str2; target starts with str1
// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
// return 0 on success, -1 on failure
/// replace str1 in target with str2; target starts with str1
/// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
/// return 0 on success, -1 on failure
#ifndef CAPSTONE_DIET
static int str_replace(char *result, char *target, const char *str1, char *str2)
{
size_t target_len = strlen(target);
size_t str1_len = strlen(str1);
if (target_len < str1_len) {
return -1;
}
// only perform replacement if the output fits into result
if (strlen(target) - strlen(str1) + strlen(str2) < CS_MNEMONIC_SIZE - 1) {
if (target_len - str1_len + strlen(str2) <= CS_MNEMONIC_SIZE - 1) {
// copy str2 to beginning of result
strcpy(result, str2);
// skip str1 - already replaced by str2
strcat(result, target + strlen(str1));
snprintf(result, CS_MNEMONIC_SIZE, "%s%s", str2, target + str1_len);
return 0;
} else