Skip to content

Commit

Permalink
Fix memory error when reading non null terminated char*
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Romain committed Feb 8, 2019
1 parent 5bbecd7 commit 2015a4c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/readheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using namespace Rcpp;

List vlrsreader(LASheader*);
List globalencodingreader(LASheader*);
inline std::string nullterminate(CHAR*, int);

// Read header in a las or laz file
//
Expand Down Expand Up @@ -243,19 +244,19 @@ List vlrsreader(LASheader* lasheader)
}
else if (vlr.record_id == 34737) // GeoAsciiParamsTag
{
lvlr.push_back(std::string(lasheader->vlr_geo_ascii_params));
lvlr.push_back(nullterminate(lasheader->vlr_geo_ascii_params, vlr.record_length_after_header));
lvlrnames.push_back("tags");
lvlrsnames.push_back("GeoAsciiParamsTag");
}
else if (vlr.record_id == 2111) // WKT OGC MATH TRANSFORM
{
lvlr.push_back(std::string(lasheader->vlr_geo_ogc_wkt_math));
lvlr.push_back(nullterminate(lasheader->vlr_geo_ogc_wkt_math, vlr.record_length_after_header));
lvlrnames.push_back("WKT OGC MATH TRANSFORM");
lvlrsnames.push_back("WKT OGC MT");
}
else if (vlr.record_id == 2112) // WKT OGC COORDINATE SYSTEM
{
lvlr.push_back(std::string(lasheader->vlr_geo_ogc_wkt));
lvlr.push_back(nullterminate(lasheader->vlr_geo_ogc_wkt, vlr.record_length_after_header));
lvlrnames.push_back("WKT OGC COORDINATE SYSTEM");
lvlrsnames.push_back("WKT OGC CS");
}
Expand Down Expand Up @@ -407,4 +408,12 @@ void lasfilterusage()
{
Rcerr << "Error: " << e.what() << std::endl;
}
}

inline std::string nullterminate(CHAR* str, int len)
{
if (str[len-1] != '\0')
return(std::string(str, str+len));
else
return(std::string(str));
}

0 comments on commit 2015a4c

Please sign in to comment.