Skip to content

Commit

Permalink
[Fix] bad allod alloc by add two try_catch
Browse files Browse the repository at this point in the history
  • Loading branch information
mUbuntu committed Jun 22, 2018
1 parent 2a2c95f commit 60d845f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
5 changes: 1 addition & 4 deletions Peanut/cn.codeyourlife.peanut/Http/HttpHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ bool HttpHandle::read()
while (true)
{
int savedErrNo;
// std::cout<< "\n报文流动1: ";
byteRead = readBuffer_.readFd(sockFd_, &savedErrNo);
if (byteRead == -1)
{
Expand Down Expand Up @@ -144,8 +143,7 @@ void HttpHandle::processRead()
std::string url = request_.path_;
url = url.substr(0, url.find_first_of('?'));
result = (*_controllerMap[url])(reqInfo);
// std::cout <<"\n报文流动3 调用后端接口: 请求方法: "<<url<<" 结果:"<<result<<"\n";
}
}
else if(!strcasecmp(request_.method_.c_str(), METHOD_POST))
{
reqInfo.package(this->clientIP_, request_.path_, request_.content_);
Expand Down Expand Up @@ -193,7 +191,6 @@ void HttpHandle::processWrite()
while (true)
{
size_t len = writeBuffer_.readAbleSize();
// std::cout<< "\n 报文流动4: 发送响应报文 "<< writeBuffer_.readLoc()<<"\n";
res = write(sockFd_, writeBuffer_.readLoc(), len);
if (res < 0)
{
Expand Down
11 changes: 7 additions & 4 deletions Peanut/cn.codeyourlife.peanut/Http/HttpReq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ bool HttpReq::parseRequestLine(Buffer &buf)
{
char method[64], path[256], version[64];
char *line = buf.getLine();
if(line == nullptr)
{
Peanut::logError("error getline");
delete[] line;
return false;
}
sscanf(line, "%s %s %s", method, path, version);
// std::cout <<"\n报文流动2 起始行: "<< method <<" "<<path<<" "<< version << "\n";
setMethod(method, strlen(method));
setPath(path);
delete[] line;
Expand Down Expand Up @@ -132,10 +137,8 @@ bool HttpReq::parseHeaders(Buffer &buf) // Other head info
// If have content_ store content_;
else if(strstr(line, "<end>"))
{
// std::cout <<"\n报文流动2: 头部"<< line<<"\n";
content_ = line;
content_ = content_.substr(0, content_.find("<end>"));
// std::cout<<"\ncontent:0 "<<content_<<"\n";
}
delete[] line;
}
Expand Down Expand Up @@ -193,4 +196,4 @@ Peanut::RequestParseState HttpReq::parseRequest(Buffer& buf)
}
}
return state_;
}
}
26 changes: 22 additions & 4 deletions Peanut/cn.codeyourlife.peanut/Http/IO/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

#include "Buffer.h"

#include "../../Log/Log.h"
#include <sys/socket.h>
#include <cstdarg>
#include <new>
#include<sys/uio.h>

/**
Expand Down Expand Up @@ -80,18 +81,35 @@ char* Buffer::getLine()
return nullptr;
}
retrieve(size);
char* res = new char[size + 5];
char* res;
try
{
res = new char[size + 5];
}
catch(const std::bad_alloc &memExp)
{
res = nullptr;
Peanut::logError(memExp.what());
}
std::copy(start, start + size, res);
const char* flag="<end>";
std::copy(flag,flag+5, res+size);
// std::cout<< "\nres: "<< res << "\n";
return res;
}
else
{
size = end - start - 1;
retrieve(size + 2);
char* res = new char[size];
char* res;
try
{
res = new char[size];
}
catch(const std::bad_alloc &memExp)
{
res = nullptr;
Peanut::logError(memExp.what());
}
std::copy(start, start + size, res);
return res;
}
Expand Down
Loading

0 comments on commit 60d845f

Please sign in to comment.