functions.c: fix backtrace handler

distroquery.cpp: fix sigsegv when PATH_INFO environment var is not set
This commit is contained in:
Silvan Calarco 2024-07-09 11:15:22 +02:00
parent be93c2bdad
commit 210494ea54
2 changed files with 12 additions and 66 deletions

View File

@ -1537,11 +1537,11 @@ main(int argc, char *argv[])
path_info = getenv("PATH_INFO");
query_string = getenv("QUERY_STRING");
if (strncmp(path_info, "/api/v1/", strlen(API_PREFIX)) == 0) {
if (path_info && strncmp(path_info, "/api/v1/", strlen(API_PREFIX)) == 0) {
DistroqueryAPI *api = new DistroqueryAPI(firstconfigtag);
api->getApiResponse(&path_info[strlen(API_PREFIX)]);
exit(0);
} else if (strlen(path_info) > 0) {
} else {
cout << "Content-Type: text/html;charset=utf-8" << endl;
cout << "Status: 400 Bad request" << endl << endl;
cout << "ERROR: invalid path: " << path_info;

View File

@ -23,6 +23,7 @@
#include <sys/stat.h>
#include <dirent.h>
#include <libiberty/libiberty.h>
#include <execinfo.h>
/* Tell glibc's <time.h> to provide a prototype for strptime() */
#ifndef __USE_XOPEN
@ -44,71 +45,16 @@ static struct configDefaults configdefaults;
static struct configTag *firstconfigtag = NULL;
/* Obtain a backtrace and print it to stdout. */
static void debugInfo(FILE *out, const void* ip)
{
char *debuginfo_path=NULL;
void backtraceHandler(int sig) {
void *array[10];
size_t size;
Dwfl_Callbacks callbacks={
.find_elf=dwfl_linux_proc_find_elf,
.find_debuginfo=dwfl_standard_find_debuginfo,
.debuginfo_path=&debuginfo_path,
};
// get void*'s for all entries on the stack
size = backtrace(array, 10);
Dwfl* dwfl=dwfl_begin(&callbacks);
assert(dwfl!=NULL);
assert(dwfl_linux_proc_report (dwfl, getpid())==0);
assert(dwfl_report_end (dwfl, NULL, NULL)==0);
Dwarf_Addr addr = (uintptr_t)ip;
Dwfl_Module* module=dwfl_addrmodule (dwfl, addr);
const char* function_name = dwfl_module_addrname(module, addr);
fprintf(out, "%s(",function_name);
Dwfl_Line *line=dwfl_getsrc (dwfl, addr);
if(line!=NULL)
{
int nline;
Dwarf_Addr addr;
const char* filename=dwfl_lineinfo (line, &addr,&nline,NULL,NULL,NULL);
fprintf(out, "%s:%d",strrchr(filename,'/')+1,nline);
}
else
{
const char *module_name = dwfl_module_info(module,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
fprintf(out, "in %s", module_name);
}
}
void backtraceHandler(int sig)
{
unw_context_t uc;
unw_getcontext(&uc);
unw_cursor_t cursor;
unw_init_local(&cursor, &uc);
fprintf(stderr, "*** Received signal %d; stack trace: ***\n", sig);
while(unw_step(&cursor)>0) {
unw_word_t ip;
unw_get_reg(&cursor, UNW_REG_IP, &ip);
unw_word_t offset;
char name[32];
assert(unw_get_proc_name(&cursor, name,sizeof(name), &offset)==0);
fprintf(stderr, "\tat ");
debugInfo(stderr, (void*)(ip-4));
fprintf(stderr, ")\n");
if(strcmp(name,"main")==0)
break;
}
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}