Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Segmentation fault SIGSEGV



  • Hey all. I am using ubuntu and I have a library provided by the manufacturer and when I tried to debug it, I got segmentation fault.

    My mentor said he was able to solve it by adding the following in .pro file.
    QMAKE_CXXFLAGS += -std=c++0x -pthread
    LIBS += -pthread

    But when I ran the same project in my PC, I still got the error but it was resolved in his PC.
    Can anyone please tell me where can the problem be?

    This is the function where I am getting the issue. The debugger stops at " int allocsize = strlen(key + strlen(szKey)) + 1;"

    /*! Parses the configuration file

    • \param szFile File name
    • \param szKey Key to search for
    • \param szValue Returned value (if key was found)
    • \return != 0 on success /
      /*****************************************************************************/
      static int GetConfigString(const char
      szFile, const char* szKey, char** szValue)
      {
      int ret = 0;
      FILE* fd = fopen(szFile, "r");

    if(NULL != fd)
    {
    /* File is open /
    char
    buffer = malloc(PARSER_BUFFER_SIZE);

    /* Read file line by line */
    while(NULL != fgets(buffer, PARSER_BUFFER_SIZE, fd))
    {
      char* key;
    
      /* '#' marks a comment line in the device.conf file */
      if(buffer[0] == '#')
        continue;
    
      /* Search for key in the input buffer */
      key = (char*)strcasestr(buffer, szKey);
    
      if(NULL != key)
      {
        /* We've found the key */
        int   allocsize  = strlen(key + strlen(szKey)) + 1;
        int   valuelen;
        char* tempstring = (char*)malloc(allocsize);
    
        strcpy(tempstring, key + strlen(szKey));
        valuelen = strlen(tempstring);
    
        /* strip all trailing whitespaces */
        while( (tempstring[valuelen - 1] == '\n') ||
               (tempstring[valuelen - 1] == '\r') ||
               (tempstring[valuelen - 1] == ' ') )
        {
          tempstring[valuelen - 1] = '\0';
          --valuelen;
        }
    
        *szValue = tempstring;
        ret = 1;
        break;
      }
    }
    
    free(buffer);
    fclose(fd);
    

    }

    return ret;
    }


  • Lifetime Qt Champion

    @sudharsan10 said in Segmentation fault SIGSEGV:

    I got segmentation fault

    Please post the stack trace



  • Hi @jsulm Do you mean this?

    e84bac62-46db-4c47-a15b-e98fd5c53590-image.png


  • Lifetime Qt Champion

    @sudharsan10 What is in TCP_Connector.c at line 729?
    Also check your code in nextTransport_demo.c line 200
    And I don't see anything related to Qt...



  • You shall find line 729 in the image and also in the code I have posted above :)
    I was wondering if it could be something related to my project settings, Since there was no issue while running the same project in another PC.



  • @sudharsan10

    • Your question seems to have nothing at all to do with Qt.

    • If the reason the changed link flags make things work is to do with pthread, you have said nothing about whether your program uses any threads.

    • If any line is longer than PARSER_BUFFER_SIZE your code will not work correctly.

    • int allocsize = strlen(key + strlen(szKey)) + 1;
      This line looks potentially dodgy. Print out allocsize, and consider how you are using that malloc()ed area to copy things into.

    • /* strip all trailing whitespaces */
      What happens in your code if the value is all whitespace? I do not see why your should not incorrectly keep decrementing and go too far to the left, potentially overwriting unallocated memory?

    • Since there was no issue while running the same project in another PC.

    More likely than a project/compilation issue is that your code, somewhere, uses an uninitialized value or overwrites/underwrites a random area of memory. This can and will behave/show up differently from one PC to another, for no discernible reason....



  • @JonB Thank you for pointing out at the potential problems. For now, I found it to be a project/compilation issue. I changed the compiler version to Clang just like the other PC. Also I changed the debugger settings as suggested here https://forum.qt.io/topic/100958/catching-unix-interrupt-signal-on-console-application-when-debugging-with-qtcreator/3 and it seems to work.


Log in to reply