Results 1 to 9 of 9

Thread: how to use strstr

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to use strstr

    Hello,

    This code crashes with segfault
    Qt Code:
    1. QString MainWindowImpl::getIp(QString line)
    2. {
    3. static char ip[1000], s[1000];
    4. strcpy(s, line.toLocal8Bit().constData());
    5. char *p1 = strstr(s, "client ") + 7, *p2 = ip;
    6. while (*p1 && *p1 != ']')
    7. *p2++ = *p1++;
    8. *p2 = 0;
    9. return ip;
    To copy to clipboard, switch view to plain text mode 

    If I change the line with strstr to:
    Qt Code:
    1. char *p1 = s + 7, *p2 = ip;
    To copy to clipboard, switch view to plain text mode 
    all is well..

    I know I can do things the qt way but I want to be portable with plain c strings.
    Thanks.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use strstr

    I know I can do things the qt way but I want to be portable with plain c strings.
    But you start with a QString anyway, so why not stay in the Qt realm?

    Ginsengelf

  3. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use strstr

    That is explained in the post.

    Quote Originally Posted by Ginsengelf View Post
    But you start with a QString anyway, so why not stay in the Qt realm?

    Ginsengelf

  4. #4
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: how to use strstr

    I'd use strncpy to copy the exact length of the string.

    And don't forget to add a NULL in the end, otherwise strstr won't likely work.

  5. #5
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use strstr

    Qt Code:
    1. int len = line.length();
    2. strncpy(s, line.toLocal8Bit().constData(), len);
    3. s[len] = 0;
    4. char *p1 = strstr(s, "client ") + 7, *p2 = ip;
    To copy to clipboard, switch view to plain text mode 

    Still segfaults. I would have been very surprised if that was the solution, a normal strcpy wouldn't work and a strncpy would?
    Last edited by JeanC; 20th August 2008 at 17:43.

  6. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: how to use strstr

    did you try replacing

    Qt Code:
    1. line.toLocal8Bit().constData()
    To copy to clipboard, switch view to plain text mode 

    by

    Qt Code:
    1. line.toStdString().c_str()
    To copy to clipboard, switch view to plain text mode 

    Is it still crashing on that line :
    Qt Code:
    1. char *p1 = strstr(s, "client ") + 7, *p2 = ip;
    To copy to clipboard, switch view to plain text mode 
    ?
    why do you declare
    Qt Code:
    1. static char ip[1000], s[1000];
    To copy to clipboard, switch view to plain text mode 
    as static ?
    Last edited by bunjee; 20th August 2008 at 19:05.

  7. #7
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use strstr

    Qt Code:
    1. line.toStdString().c_str()
    To copy to clipboard, switch view to plain text mode 

    Just tried. Same result. From the posts I've read the only safe way is with those calls I used, as I understood it, all the rest leaves dangling pointers.

    why do you declare
    Qt Code:
    1. static char ip[1000], s[1000];
    To copy to clipboard, switch view to plain text mode 
    as static ?
    Oops that's typo.

    I thought Qt was about c++, I wonder what it takes to have it use charpointers, maybe some voodoo? Sigh. I don't understand why this is so hard. There's tons of c code out there, I have still useful code I wrote 25 years ago, I'm not gonna qchar or qbytearray all that. Sorry for the rant.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use strstr

    Quote Originally Posted by JeanC View Post
    I know I can do things the qt way but I want to be portable with plain c strings.
    How does using strstr on a string ripped from QString make you "portable with plain c strings"?

    I'd check strstr() result for NULL and divide those statements separated by commas into two (or even three if you consider the "+7" part) real statements if I were you...

  9. #9
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use strstr

    Hey wysota,

    I'd check strstr() result for NULL
    I don't even have to try out the code, not checking for NULL from strstr is the error.

    That is basic stuff, how can I forget that, I'm getting too old for this, I have to try another hobby like go fishing or so.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.