Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:13:14+00:00 2026-05-13T16:13:14+00:00

I have been getting a really annoying error about an std::out_of_range when calling substr.

  • 0

I have been getting a really annoying error about an std::out_of_range when calling substr. The exact error is

terminate called after throwing an
instance of ‘std::out_of_range’
what(): basic_string::substr

I’m absolutely sure that tmp_request has a length greater then 1. No matter what I pass to substr—1, 2, or bodypos—it always throws that error. I’m using g++ on Unix.

Only interesting thing I can include is the string has multiple "\r\n", including one "\r\n\r\n".

In one cpp file:

std::string tmp_request, outRequest;

tmp_request = SS_Twitter->readData();
outRequest = SS_Twitter->parse(tmp_request);

In another:

 std::string parse(const std::string &request)
 {
  std::map<std::string,std::string> keyval;
  std::string outRequest;
  if(request[0]=='P')
  {
   if(request.find("register")!=std::string::npos)
   { //we have a register request
    size_t bodypos = request.find("username");
    if(bodypos==std::string::npos) 
    {
     HttpError(400,"Malformed HTTP POST request. Could not find key username.",request); 
    }
    else
    {
     std::string body = request.substr(bodypos);
     StringExplode(body,"&", "=",keyval);
     outRequest = "doing stuff";
    }

   }

Update:

std::string request2("P\r\nregister\r\nusername=hello\r\n\r\n");

std::string body = request2.substr(4);

That throws the same error. Now I know this is perfectly valid and correct code, but it’s still throwing the error.
//removed source link

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:13:15+00:00Added an answer on May 13, 2026 at 4:13 pm

    I modified your sample slightly to decrease amount of indentation used.
    There are 5 “test cases” and none causes any problem. Could you please provide a sample request to reproduce the problem you’re having.

    EDIT: Forgot to mention: if this sample as it is (with commented-out bits) doesn’t produce that error, your best bet is that you have a bug in your StringExplode function. You could post its source, to get a more helpful advice.

    EDIT2:
    In your StringExplode, change results[tmpKey] = tmpKey.substr(found+1); to results[tmpKey] = tmpResult[i].substr(found+1);. Change int found to size_t found, and remove all of if (found > 0), that will fix your mysterious out_of_range. You were substr-ing a wrong string. Just in case, here’s the code with a fix:

    void StringExplode(std::string str, std::string objseparator, std::string keyseperator,
                       std::map <std::string, std::string> &results)
    {
        size_t found;
        std::vector<std::string> tmpResult;
        found = str.find_first_of(objseparator);
        while(found != std::string::npos)
        {
            tmpResult.push_back(str.substr(0,found));
            str = str.substr(found+1);
            found = str.find_first_of(objseparator);
        }
        if(str.length() > 0)
        {
            tmpResult.push_back(str);
        }
    
        for(size_t i = 0; i < tmpResult.size(); i++)
        {
            found = tmpResult[i].find_first_of(keyseperator);
            while(found != std::string::npos)
            {
                    std::string tmpKey = tmpResult[i].substr(0, found);
                    results[tmpKey] = tmpResult[i].substr(found+1);
                    found = tmpResult[i].find_first_of(keyseperator, found + results[tmpKey].size());
            }
    
        }
    }
    

    Initial test code:

    #include <iostream>
    #include <map>
    #include <string>
    
    std::string parse(const std::string &request)
    {
        std::map<std::string,std::string> keyval;
        std::string outRequest;
    
        if(request[0] != 'P')
            return outRequest;
    
        if(request.find("register") == std::string::npos)
            return outRequest;
    
        //we have a register request
        size_t bodypos = request.find("username");
        if(bodypos==std::string::npos)
        {
            // HttpError(400,"Malformed HTTP POST request. Could not find key username.",request);
            // you said HttpError returns, so here's a return
            return outRequest;
        }
    
        std::string body = request.substr(bodypos);
        // StringExplode(body,"&", "=",keyval);
        outRequest = "doing stuff";
    
        return outRequest;
    }
    
    int main()
    {
    
        std::string request("P\r\nregister\r\nusername=hello\r\n\r\n");
        std::cout << "[" << parse(request) << "]\n";
    
        request = "Pregisternusername=hello\r\n\r\n";
        std::cout << "[" << parse(request) << "]\n";
    
        request = "Pregisternusername=hello";
        std::cout << "[" << parse(request) << "]\n";
    
        request = "registernusername=hello";
        std::cout << "[" << parse(request) << "]\n";
    
        request = "";
        std::cout << "[" << parse(request) << "]\n";
    
        return 0;
    }
    

    This outputs, predictably:

    [doing stuff]
    [doing stuff]
    [doing stuff]
    []
    []

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 394k
  • Answers 394k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This seems like a really bad idea. Instead, why don't… May 15, 2026 at 2:27 am
  • Editorial Team
    Editorial Team added an answer Run a phpinfo(), and see if those settings are accurate/to… May 15, 2026 at 2:27 am
  • Editorial Team
    Editorial Team added an answer I think you are adding the first vertex of neighbours… May 15, 2026 at 2:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.