0

Based on the first answer of this question I wrote a code to parse a QueryString of an URL like:

http://localhost/parser.cgi?key1=value1&key2=value2&key3=value3&key4=value4

I compiled that code with no errors or warnings.

And I had a 500 Internal Server Error. Then, when I runned that executable on a terminal, I got the following error:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
Aborted (`core' generated)

This is my code:

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int argc, char * argv[]) {
char * cqs = getenv("QUERY_STRING");
string qs(cqs);
string delimiter = "&";
size_t position = 0;
string token;
vector<string> parameters;
vector<string> kv;
while((position = qs.find(delimiter) != string::npos)) {
token = qs.substr(0, position);
parameters.push_back(token);
qs.erase(0, position + delimiter.length()); }
delimiter = "=";
for(int n = 0; n < parameters.size(); ++n) {
while((position = parameters[n].find(delimiter) != string::npos)) {
token = parameters[n].substr(0, position);
kv.push_back(token);
parameters[n].erase(0, position + delimiter.length()); } }
for(int s = 0; s < kv.size(); ++s) {
cout << "Key: " << kv[s] << "; Value: " << kv[++s] << ";\n"; }
return 0; }

And this was my stack trace on gdb:

#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737348256128)
        at pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140737348256128)
        at pthread_kill.c:80
#2  __GI___pthread_kill (threadid=140737348256128, signo=signo@entry=6)
        at pthread_kill.c:91
#3  0x00007ffff7b8f476 in __GI_raise (sig=sig@entry=6)
        at ../sysdeps/posix/raise.c:26
#4  0x00007ffff7b757b7 in __GI_abort () at abort.c:79
#5  0x00007ffff7e2ca31 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7e384ec in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff7e38557 in std::terminate() ()
    from /lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007ffff7e387f9 in __cxa_throw ()
    from /lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff7e2f2bb in std::__throw_logic_error(char const*) ()
    from /lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x0000555555557021 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*> (this=0x7fffffffdea0, 
        __beg=0x0, 
        __end=0x1 <error: Cannot access memory at address 0x1>)
        at /usr/include/c++/11/bits/basic_string.tcc:212
#11 0x0000555555556d79 in std::__cxx11::basic_string<char, std::char_traits<char--Type <RET> for more, q to quit, c to continue without paging--
    >, std::allocator<char> >::basic_string<std::allocator<char> > (
        this=0x7fffffffdea0, __s=0x0, __a=...)
        at /usr/include/c++/11/bits/basic_string.h:539
#12 0x000055555555667a in main (argc=1, argv=0x7fffffffe088) at pp.cpp:10

Why is this happening and how can I fix it?

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
MxtApps
  • 23
  • 4

0 Answers0