Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise custom exception when memory allocation fails #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/drw_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ enum DBG_LEVEL {
DEBUG
};

/**
* Exception raised when memory allocation fails.
*/
class MemoryAllocationException {
public:
MemoryAllocationException(std::string what)
: mWhat{what}
{}
std::string message() const throw() {return mWhat;}
private:
std::string mWhat;
};

//! Special codes for colors
enum ColorCodes {
ColorByLayer = 256,
Expand Down
29 changes: 20 additions & 9 deletions src/drw_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@

#include <cstdlib>
#include "drw_entities.h"
#include "drw_base.h"
#include "intern/dxfreader.h"
#include "intern/dwgbuffer.h"
#include "intern/drw_dbg.h"

#define RESERVE(vector,size,msg) try { \
vector.reserve(size); \
} catch(const std::exception &e) { \
std::stringstream s; \
s << msg << " size=" << size << "; error="; \
s << e.what(); \
throw DRW::MemoryAllocationException(s.str()); \
}


//! Calculate arbitrary axis
/*!
* Calculate arbitrary axis for apply extrusions
Expand Down Expand Up @@ -1165,7 +1176,7 @@ void DRW_LWPolyline::parseCode(int code, dxfReader *reader){
break;
case 90:
vertexnum = reader->getInt32();
vertlist.reserve(vertexnum);
RESERVE(vertlist,vertexnum,"vertex list")
break;
case 210:
haveExtrusion = true;
Expand Down Expand Up @@ -1200,7 +1211,7 @@ bool DRW_LWPolyline::parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs){
if (flags & 1)
extPoint = buf->getExtrusion(false);
vertexnum = buf->getBitLong();
vertlist.reserve(vertexnum);
RESERVE(vertlist,vertexnum,"vertex list")
unsigned int bulgesnum = 0;
if (flags & 16)
bulgesnum = buf->getBitLong();
Expand Down Expand Up @@ -1791,7 +1802,7 @@ void DRW_Hatch::parseCode(int code, dxfReader *reader){
break;
case 91:
loopsnum = reader->getInt32();
looplist.reserve(loopsnum);
RESERVE(looplist,loopsnum,"loop list")
break;
case 92:
loop = std::make_shared<DRW_HatchLoop>(reader->getInt32());
Expand Down Expand Up @@ -1903,12 +1914,12 @@ bool DRW_Hatch::parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs){
spline->flags |= (isRational << 2); //rational
spline->flags |= (buf->getBit() << 1); //periodic
spline->nknots = buf->getBitLong();
spline->knotslist.reserve(spline->nknots);
RESERVE(spline->knotslist,spline->nknots,"knots list")
for (dint32 j = 0; j < spline->nknots;++j){
spline->knotslist.push_back (buf->getBitDouble());
}
spline->ncontrol = buf->getBitLong();
spline->controllist.reserve(spline->ncontrol);
RESERVE(spline->controllist,spline->ncontrol,"control list")
for (dint32 j = 0; j < spline->ncontrol;++j){
std::shared_ptr<DRW_Coord> crd = std::make_shared<DRW_Coord>(buf->get3BitDouble());
spline->controllist.push_back(crd);
Expand All @@ -1918,7 +1929,7 @@ bool DRW_Hatch::parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs){
}
if (version > DRW::AC1021) { //2010+
spline->nfit = buf->getBitLong();
spline->fitlist.reserve(spline->nfit);
RESERVE(spline->fitlist,spline->nfit,"fit list")
for (dint32 j = 0; j < spline->nfit;++j){
std::shared_ptr<DRW_Coord> crd = std::make_shared<DRW_Coord>(buf->get3BitDouble());
spline->fitlist.push_back(crd);
Expand Down Expand Up @@ -2142,17 +2153,17 @@ bool DRW_Spline::parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs){
return false; //RLZ: from doc only 1 or 2 are ok ?
}

knotslist.reserve(nknots);
RESERVE(knotslist,nknots,"knots list")
for (dint32 i= 0; i<nknots; ++i){
knotslist.push_back (buf->getBitDouble());
}
controllist.reserve(ncontrol);
RESERVE(controllist,ncontrol,"control list")
for (dint32 i= 0; i<ncontrol; ++i){
controllist.push_back(std::make_shared<DRW_Coord>(buf->get3BitDouble()));
if (weight)
DRW_DBG("\n w: "); DRW_DBG(buf->getBitDouble()); //RLZ Warning: D (BD or RD)
}
fitlist.reserve(nfit);
RESERVE(fitlist,nfit,"fit list")
for (dint32 i= 0; i<nfit; ++i)
fitlist.push_back(std::make_shared<DRW_Coord>(buf->get3BitDouble()));

Expand Down
13 changes: 11 additions & 2 deletions src/drw_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
#include "intern/drw_dbg.h"
#include "intern/dwgutil.h"

#define RESERVE(vector,size,msg) try { \
vector.reserve(size); \
} catch(const std::exception &e) { \
std::stringstream s; \
s << msg << " size=" << size << "; error="; \
s << e.what(); \
throw DRW::MemoryAllocationException(s.str()); \
}

//! Base class for tables entries
/*!
* Base class for tables entries
Expand Down Expand Up @@ -429,7 +438,7 @@ void DRW_LType::parseCode(int code, dxfReader *reader){
case 73:
size = reader->getInt32();
path.clear();
path.reserve(size);
RESERVE(path,size,"path")
break;
case 40:
length = reader->getDouble();
Expand Down Expand Up @@ -703,7 +712,7 @@ bool DRW_Block_Record::parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs
DRW_DBG("flags: "); DRW_DBG(flags); DRW_DBG(", ");
if (version > DRW::AC1015) {//2004+ fails in 2007
objectCount = buf->getBitLong(); //Number of objects owned by this block
entMap.reserve(objectCount);
RESERVE(entMap,objectCount,"entity map")
}
basePoint.x = buf->getBitDouble();
basePoint.y = buf->getBitDouble();
Expand Down