Skip to content

Commit

Permalink
Fix error handling after calloc calls that may ask for 0 bytes
Browse files Browse the repository at this point in the history
Close #1980
  • Loading branch information
hillu committed Oct 3, 2023
1 parent d399826 commit 8e5584b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
17 changes: 13 additions & 4 deletions libyara/modules/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ define_function(log_string)
// Assume the entire string is non-printable, so allocate 4 times the
// space so that we can represent each byte as an escaped value. eg: \x00
// Add an extra byte for the NULL terminator.
char* msg = (char*) yr_calloc((s->length * 4) + 1, sizeof(char));
if (msg == NULL)
return_integer(YR_UNDEFINED);
char* msg;
if (s->length == 0)
{
callback(ctx, CALLBACK_MSG_CONSOLE_LOG, (void*) "", ctx->user_data);
return_integer(1);
}
else
{
msg = (char*) yr_calloc((s->length * 4) + 1, sizeof(char));
if (msg == NULL)
return_integer(YR_UNDEFINED);
}

char* p = msg;
for (size_t i = 0; i < s->length; i++)
Expand Down Expand Up @@ -86,7 +95,7 @@ define_function(log_string_msg)
// Add an extra byte for the NULL terminator.
size_t msg_len = strlen(m) + (s->length * 4) + 1;
char* msg = (char*) yr_calloc(msg_len, sizeof(char));
if (msg == NULL)
if (msg == NULL && msg_len > 0)
return_integer(YR_UNDEFINED);

char* p = msg;
Expand Down
2 changes: 1 addition & 1 deletion libyara/modules/dotnet/dotnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ static bool parse_method_params(
// Array to hold all the possible parameters
PARAMETERS* params = yr_calloc(param_count, sizeof(PARAMETERS));

if (!params)
if (params == NULL && param_count > 0)
return false;

for (uint32_t idx = 0; idx < param_count; ++idx)
Expand Down
2 changes: 1 addition & 1 deletion libyara/modules/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ define_function(telfhash)
int symbol_count = 0;
char** clean_names = yr_calloc(list->count, sizeof(*clean_names));

if (!clean_names)
if (clean_names == NULL && list->count > 0)
return_string(YR_UNDEFINED);

for (ELF_SYMBOL* i = list->symbols; i != NULL; i = i->next)
Expand Down
2 changes: 1 addition & 1 deletion libyara/modules/pe/authenticode-parser/certificate.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ CertificateArray* parse_signer_chain(X509* signCert, STACK_OF(X509) * certs)
goto error;

result->certs = (Certificate**) calloc(certCount, sizeof(Certificate*));
if (!result->certs)
if (!result->certs && certCount > 0)
goto error;

/* Convert each certificate to internal representation */
Expand Down
6 changes: 3 additions & 3 deletions libyara/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ YR_API int yr_scanner_create(YR_RULES* rules, YR_SCANNER** scanner)
new_scanner->rule_evaluate_condition_flags == NULL ||
new_scanner->ns_unsatisfied_flags == NULL ||
new_scanner->strings_temp_disabled == NULL ||
new_scanner->matches == NULL || //
new_scanner->unconfirmed_matches == NULL)
(new_scanner->matches == NULL && rules->num_strings > 0) ||
(new_scanner->unconfirmed_matches == NULL && rules->num_strings > 0))
{
yr_scanner_destroy(new_scanner);
return ERROR_INSUFFICIENT_MEMORY;
Expand All @@ -294,7 +294,7 @@ YR_API int yr_scanner_create(YR_RULES* rules, YR_SCANNER** scanner)
new_scanner->profiling_info = yr_calloc(
rules->num_rules, sizeof(YR_PROFILING_INFO));

if (new_scanner->profiling_info == NULL)
if (new_scanner->profiling_info == NULL && rules->num_rules > 0)
{
yr_scanner_destroy(new_scanner);
return ERROR_INSUFFICIENT_MEMORY;
Expand Down

0 comments on commit 8e5584b

Please sign in to comment.