summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstderr64 <stderr64@xservers.dy.fi>2024-04-05 22:15:29 +0300
committerstderr64 <stderr64@xservers.dy.fi>2024-04-05 22:15:29 +0300
commit70a0103bc0f82ed13ded8abd7ef926358d816179 (patch)
tree32057fe33f2093ad23f95c6857122a0954f3b71d
parentd86ca619cf532a010198dfa0d9f5ed8f29f0d673 (diff)
downloadCWebHook-70a0103bc0f82ed13ded8abd7ef926358d816179.tar.gz
CWebHook-70a0103bc0f82ed13ded8abd7ef926358d816179.tar.zst
More checks to make sure memory is allocated successfully and safer length checks
-rw-r--r--webhook_exec.h19
1 files changed, 13 insertions, 6 deletions
diff --git a/webhook_exec.h b/webhook_exec.h
index 48b9a4b..5b7512a 100644
--- a/webhook_exec.h
+++ b/webhook_exec.h
@@ -38,16 +38,23 @@ char *webhook_exec( webhooks_data_t *wh_data, char *wh_endpoint ){
}
size_t read_bytes = 0;
char *current_line = (char*)calloc( 8192, sizeof(char) );
- while ( fgets(current_line, 8192 * sizeof(char), wh_proc) != NULL ){
- if ( (size_t)((size_t)strlen(current_line) + read_bytes) > (size_t)MAX_WEBHOOK_OUTPUT_LENGTH )
+ if ( current_line == NULL ){
+ free( wh_data->wh_output );
+ wh_data->wh_output = NULL;
+ wh_data->wh_command = NULL;
+ wh_data->wh_exec_data = NULL;
+ return "Error: failed to allocate memory for reading command output";
+ }
+ while ( fgets(current_line, (size_t)(8192 * sizeof(char)), wh_proc) != NULL ){
+ if ( (size_t)((size_t)strnlen(current_line, (size_t)(8192 * sizeof(char))) + read_bytes) > (size_t)MAX_WEBHOOK_OUTPUT_LENGTH )
break;
- strncat( wh_data->wh_output, current_line, (size_t)strlen(current_line) );
- read_bytes += (size_t)strlen( current_line );
- memset( current_line, 0, 8192 * sizeof(char) );
+ strncat( wh_data->wh_output, current_line, (size_t)strnlen(current_line, (size_t)(8192 * sizeof(char))) );
+ read_bytes += (size_t)strnlen( current_line, (size_t)(8192 * sizeof(char)) );
+ memset( current_line, 0, (size_t)(8192 * sizeof(char)) );
}
free( current_line );
pclose( wh_proc );
- if ( wh_data->wh_output == NULL || (size_t)strlen(wh_data->wh_output) <= 0 )
+ if ( wh_data->wh_output == NULL || (size_t)strnlen(wh_data->wh_output, (size_t)MAX_WEBHOOK_OUTPUT_LENGTH) <= 0 )
return "Error: failed to get output";
return wh_data->wh_output;
}