Hello Bug 2024-06-08 19:59 采纳率: 100%
浏览 6
已结题

C++[19:51:18 (18438)][SgiParser.cpp:158][ERROR] Exception: basic_string::substr

请教老哥一个C++的问题
错误问题
[19:51:18 (18438)][SgiParser.cpp:158][ERROR] Exception: basic_string::substr

这是SgiParser.cpp:158代码

/*************************************************************************
    > Date: Mon 14 Dec 2015 05:41:31 PM CST
    > Author: Simon
    > Desc:
*************************************************************************/

#include "SgiParser.h"
#include <string>
#include "libredis/RedisClient.h"
#include "jsoncpp/json.h"
#include "Command.h"
#include "CommandMgr.h"
#include "libcrypto/UrlEncode.h"

CSgiParser::CSgiParser(uint32 nBufferSize):CHttpConnection(nBufferSize)
{
    m_sRlpStr = "/v2/live/rlp";
    m_sInternalKeepalive = "/fkeepalive";
}

CSgiParser::~CSgiParser()
{
    m_mCommands = NULL;
}

void CSgiParser::Init()
{
    m_mCommands = CCommandMgr::GetCommandPtr(m_nIndex);
    dzlog_info("m_mCommands : %p, nIndex=%d\n", m_mCommands, int32(m_nIndex) );
}

void CSgiParser::Handle()
{
    try
    {
        dzlog_debug("Entering Handle method");

        if (NULL == m_mCommands)
        {
            SendCode(500);
            dzlog_error("m_mCommands is NULL");
            return;
        }

        ResetLast();

        STR sUri;
        GetRequestUri(sUri);
        dzlog_debug("Request URI: %s", sUri.c_str());

        // Parse command from URI
        std::size_t pos = sUri.find('?');
        if (pos == std::string::npos)
            m_sCommand = sUri;
        else
            m_sCommand = sUri.substr(0, pos);
        
        dzlog_debug("Parsed command: %s", m_sCommand.c_str());

        // Handle keepalive request
        if (m_sCommand == m_sInternalKeepalive)
        {
            SendCode(200);
            dzlog_debug("Keepalive request handled");
            return;
        }

        // Special handling for rlp interface
        STR sCheckStr = m_sCommand;
        std::string::size_type idx = m_sCommand.find(m_sRlpStr);
        if (idx != std::string::npos)
        {
            sCheckStr = m_sRlpStr;
        }

        SetInterface(sCheckStr);
        dzlog_debug("Interface set to: %s", sCheckStr.c_str());

        // Check if the command is defined
        Map_RequestCommand::iterator itFind = m_mCommands->find(sCheckStr);
        if (itFind == m_mCommands->end())
        {
            itFind = m_mCommands->find("/");
            if (itFind == m_mCommands->end())
            {
                dzlog_error("Command not found: %s", sCheckStr.c_str());
                SendCode(404);
                return;
            }
        }

        InitUrlIndex();
        dzlog_debug("URL index initialized");

        // Parse parameters
        if (sUri.size() > m_sCommand.size())
        {
            STR sParams = sUri.substr(m_sCommand.size() + 1);
            dzlog_debug("Request parameters: %s", sParams.c_str());

            std::size_t pos1;
            while ((pos1 = sParams.find('=')) != std::string::npos)
            {
                STR sKey = sParams.substr(0, pos1);
                sParams = sParams.substr(sKey.size() + 1);

                STR sValue;
                std::size_t pos2 = sParams.find('&');
                if (pos2 == std::string::npos)
                {
                    sValue = sParams;
                    sParams = "";
                }
                else
                {
                    sValue = sParams.substr(0, pos2);
                    sParams = sParams.substr(sValue.size() + 1);
                }

                // Decrypt
                CCryptoBase* pCryptor = itFind->second->GetCryptor();
                if (pCryptor == NULL)
                {
                    dzlog_error("CCryptoBase::Create failed");
                    SendCode(500);
                    return;
                }

                sValue = url_decode(sValue);
                STR sRandStr;
                sValue = pCryptor->Decrypto(sKey.c_str(), sValue.c_str(), sValue.length(), NULL, sRandStr);
                if (!strcmp(sKey.c_str(), "i"))
                {
                    m_sIRandStr = sRandStr;
                }
                else if (!strcmp(sKey.c_str(), "e"))
                {
                    m_sERandStr = sRandStr;
                }

                dzlog_debug("Parameter: %s = %s", sKey.c_str(), sValue.c_str());
                m_mapParam[sKey] = sValue;
            }
        }

        dzlog_debug("Parameters parsed and decrypted");

        itFind->second->Reset(this);
        dzlog_debug("Command reset");
        itFind->second->Handle();
        dzlog_debug("Command handled");
        AddHandleFinishCnt();
        dzlog_debug("Handle method finished successfully");
    }
    catch (const std::exception& e)
    {
        SendCode(500);
        // 输出详细的字符串处理信息
        dzlog_error("String processing error occurred:");
        dzlog_error("Request URI: %s", sUri.c_str());
        dzlog_error("Parsed command: %s", m_sCommand.c_str());
        dzlog_error("Command substring index: %zu", pos);
        dzlog_error("Command length: %zu", m_sCommand.size());
        dzlog_error("Exception: %s", e.what());

    }
    catch (...)
    {
        SendCode(500);
        dzlog_error("Unknown exception caught");
    }
}

void CSgiParser::ResetLast()
{
    m_sCommand = "";
    m_mapParam.clear();
}

STR CSgiParser::GetUriIdxContent(int32 idx)
{
    if (idx >= (int32)m_vUriIdxVec.size())
    {
        return "";
    }
    return m_vUriIdxVec[idx];
}

void CSgiParser::InitUrlIndex()
{
    StdStrUtils::SplitString(m_sCommand, m_vUriIdxVec, "/");
}

void CSgiParser::InitFileID()
{
    size_t index = m_sCommand.find_last_of("/");
    dzlog_debug("m_sCommand: %zu", m_sCommand);
    dzlog_debug("Index: %zu, String size: %zu", index, m_sCommand.size());

    if (index != std::string::npos && index + 1 < m_sCommand.size()) {
        STR tmp = m_sCommand.substr(index + 1);
        dzlog_debug("File ID: %s", tmp.c_str()); // 在这里添加打印语句
    } else {
        dzlog_error("Invalid index for substr: %zu", index);
    }
}

STR CSgiParser::GetCommand() const
{
    return m_sCommand;
}

STR CSgiParser::GetQuery(const SGI_STRING &sQuery) const
{
    Map_Str::const_iterator iter = m_mapParam.find(sQuery);
    if( iter != m_mapParam.end() )
        return iter->second;
    return "";
}


  • 写回答

3条回答 默认 最新

  • Hello Bug 2024-06-12 20:08
    关注

    差不多解决了,是我代码里面有一个substr截取有问题,导致下标越界,还是得自己仔细看代码,这是一个垃圾bug问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月20日
  • 已采纳回答 6月12日
  • 创建了问题 6月8日

悬赏问题

  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件