Skip to content

Commit

Permalink
Fix bug in magic module when libmagic returns null pointer.
Browse files Browse the repository at this point in the history
The pointer returned from `libmagic` was passed directly to `yr_strdup` (a wrapper of `strdup`), but this function has undefined behavior when the argument is null.
  • Loading branch information
plusvic committed Feb 8, 2024
1 parent e4372d3 commit 3342aa0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libyara/modules/magic/magic.c
Expand Up @@ -111,8 +111,10 @@ define_function(magic_mime_type)
{
magic_setflags(cache->magic_cookie, MAGIC_MIME_TYPE);

cache->cached_mime_type = yr_strdup(
magic_buffer(cache->magic_cookie, block_data, block->size));
const char* type = magic_buffer(
cache->magic_cookie, block_data, block->size);

cache->cached_mime_type = (type == NULL) ? NULL : yr_strdup(type);
}
}

Expand Down Expand Up @@ -148,8 +150,10 @@ define_function(magic_type)
{
magic_setflags(cache->magic_cookie, 0);

cache->cached_type = yr_strdup(
magic_buffer(cache->magic_cookie, block_data, block->size));
const char* type = magic_buffer(
cache->magic_cookie, block_data, block->size);

cache->cached_type = (type == NULL) ? NULL : yr_strdup(type);
}
}

Expand Down

0 comments on commit 3342aa0

Please sign in to comment.