Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a possible UB inside of asprintf #179

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ endif
####### Compiler options

override LDFLAGS += -L$(LIBPE) -lpe -lcrypto -lssl -ldl -lm
override CFLAGS += -O2 -ffast-math -I$(LIBPE)/include -I"../include" -W -Wall -Wextra -std=c99 -pedantic
override CFLAGS += -O2 -ffast-math -I$(LIBPE)/include -I"../include" -W -Wall -Wextra -Wno-implicit-fallthrough -std=c99 -pedantic

# To compile for production define the symbol NDEBUG before invoking this makefile.
override CPPFLAGS += \
Expand Down
10 changes: 7 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static int _load_config_and_parse(pev_config_t * const config, const char *path,
while ( getline( &line, &size, fp ) != -1 )
{
// remove newline
if ( p = strrchr( line, '\n' ) ) *p = 0;
if ((p = strrchr( line, '\n')) != NULL) *p = '\0';

p = pe_utils_str_inplace_trim(line);

Expand Down Expand Up @@ -115,25 +115,29 @@ int asprintf( char **pp, char *fmt, ... )
{
char *p;
int size;
va_list args;
va_list args, args_safe;

va_start( args, fmt );
va_copy( args_safe, args );

// Just get the string size.
if ( ( size = vsnprintf( NULL, 0, fmt, args ) ) < 0 )
if ( ( size = vsnprintf( NULL, 0, fmt, args_safe ) ) < 0 )
{
va_end( args_safe );
va_end( args );
return -1;
}

if ( ! ( p = malloc( size + 1 ) ) )
{
va_end( args_safe );
va_end( args );
return -1;
}

vsprintf( *pp = p, fmt, args );

va_end( args_safe );
va_end( args );

return size;
Expand Down