From d06b01e4e40aeb9f5d30af9f2edcf76717a1ce84 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 28 May 2011 18:55:06 +0000 Subject: [PATCH] - bugfix to harden the code that looks up line #'s from a stack trace --- source/glest_game/main/main.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 66689ef1..3f2c1aef 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -297,33 +297,41 @@ public: #if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) static int getFileAndLine(void *address, char *file, size_t flen) { int line=-1; - static char buf[256]=""; + const int maxbufSize = 1024; + static char buf[maxbufSize+1]=""; char *p=NULL; // prepare command to be executed // our program need to be passed after the -e parameter //sprintf (buf, "/usr/bin/addr2line -C -e ./a.out -f -i %lx", addr); - sprintf (buf, "addr2line -C -e %s -f -i %p",application_binary.c_str(),address); + sprintf(buf, "addr2line -C -e %s -f -i %p",application_binary.c_str(),address); FILE* f = popen (buf, "r"); - if (f == NULL) { perror (buf); return 0; } // get function name - char *ret = fgets (buf, 256, f); - // get file and line - ret = fgets (buf, 256, f); + char *ret = fgets (buf, maxbufSize, f); + if(ret == NULL) { + pclose(f); + return 0; + } - if (buf[0] != '?') { - int l; + // get file and line + ret = fgets (buf, maxbufSize, f); + if(ret == NULL) { + pclose(f); + return 0; + } + + if(strlen(buf) > 0 && buf[0] != '?') { + //int l; char *p = buf; // file name is until ':' - while (*p != ':') - { + while(*p != 0 && *p != ':') { p++; } @@ -333,7 +341,7 @@ public: sscanf (p,"%d", &line); } else { - strcpy (file,"unkown"); + strcpy (file,"unknown"); line = 0; } pclose(f);