fix: return strdup instead of string literal in popen error paths in tools.h

The error return paths in tool_function_bash were returning string literals instead of heap-allocated copies via strdup, causing potential null pointer dereference when callers attempt to free the returned pointer. This fix ensures both "Popen failed!" and "Failed to allocate memory!" error messages are properly duplicated on the heap before being returned.
This commit is contained in:
retoor 2025-03-03 08:52:28 +00:00
parent 00c45e0978
commit 0bc2698d83

View File

@ -41,7 +41,7 @@ char * tool_function_bash(char * command){
fp = popen(command, "r");
if (fp == NULL) {
perror("popen failed");
return "Popen failed!";
return strdup("Popen failed!");
}
// Read output in chunks
@ -52,7 +52,7 @@ char * tool_function_bash(char * command){
perror("realloc failed");
free(output);
pclose(fp);
return "Failed to allocate memory!";
return strdup("Failed to allocate memory!");
}
output = new_output;
strcpy(output + total_size, buffer);