From 210494ea54b930e9a0fc941c45b75f190efdf26e Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Tue, 9 Jul 2024 11:15:22 +0200 Subject: [PATCH] functions.c: fix backtrace handler distroquery.cpp: fix sigsegv when PATH_INFO environment var is not set --- src/distroquery.cpp | 4 +-- src/functions.c | 74 ++++++--------------------------------------- 2 files changed, 12 insertions(+), 66 deletions(-) diff --git a/src/distroquery.cpp b/src/distroquery.cpp index 939211a..1f1940f 100644 --- a/src/distroquery.cpp +++ b/src/distroquery.cpp @@ -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; diff --git a/src/functions.c b/src/functions.c index a418d61..ca052c9 100644 --- a/src/functions.c +++ b/src/functions.c @@ -23,6 +23,7 @@ #include #include #include +#include /* Tell glibc's to provide a prototype for strptime() */ #ifndef __USE_XOPEN @@ -44,72 +45,17 @@ 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; - } - exit(1); + // print out all the frames to stderr + fprintf(stderr, "Error: signal %d:\n", sig); + backtrace_symbols_fd(array, size, STDERR_FILENO); + exit(1); } struct configTag* findRepositoryByTag(const char *tag)