1
0
Fork 0

safety: prevent buffer overflows

This commit is contained in:
scttgs0 2023-03-27 00:24:55 -05:00
parent cdb88ae745
commit 77920101d3
7 changed files with 667 additions and 660 deletions

View file

@ -201,7 +201,7 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
tglightcontour_list rwy_lights;
char debug_root[32];
sprintf(debug_root, "./airport_dbg/%s/", icao.c_str() );
snprintf(debug_root, 32, "./airport_dbg/%s/", icao.c_str() );
// parse main airport information
double apt_lon = 0.0, apt_lat = 0.0;
@ -599,13 +599,13 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
base_poly = tgPolygon::AddColinearNodes( base_poly, tmp_pvmt_nodes );
// base_poly = tgPolygon::Snap( base_poly, gSnap );
// Finally find slivers in base
//slivers.clear();
//tgPolygon::RemoveSlivers( base_poly, slivers );
//tgPolygon::MergeSlivers( rwy_polys, slivers );
//tgPolygon::MergeSlivers( pvmt_polys, slivers );
#if 0
// Then snap rwy and pavement to grid (was done right after adding intermediate nodes...)
for ( unsigned int k = 0; k < rwy_polys.size(); ++k )
@ -625,14 +625,14 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
pvmt_polys[k] = poly;
}
#endif
cleanup_end.stamp();
cleanup_time = cleanup_end - cleanup_start;
/* before tessellating the base, make sure there are no
intersecting contours */
base_poly = tgPolygon::Simplify( base_poly );
triangulation_start.stamp();
// tesselate the polygons and prepair them for final output
@ -645,7 +645,7 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
TG_LOG(SG_GENERAL, SG_DEBUG, "contours before " << rwy_polys[i].Contours() << " total points before = " << rwy_polys[i].TotalNodes());
rwy_polys[i].Tesselate();
#if 0
// dump the triangles for each poly
char desc[128];
@ -669,7 +669,7 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
TG_LOG(SG_GENERAL, SG_DEBUG, "contours before " << pvmt_polys[i].Contours() << " total points before = " << pvmt_polys[i].TotalNodes());
pvmt_polys[i].Tesselate();
#if 0
#if 0
char desc[128];
sprintf( desc, "poly_%06d", i );
tgShapefile::FromTriangles( pvmt_polys[i], debug_root, "pvmt_polys", desc );
@ -690,7 +690,7 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
TG_LOG(SG_GENERAL, SG_DEBUG, "contours before " << line_polys[i].Contours() << " total points before = " << line_polys[i].TotalNodes());
line_polys[i].Tesselate();
#if 0
char desc[128];
sprintf( desc, "poly_%06d", i );
@ -701,11 +701,11 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
line_polys[i].Texture();
}
}
TG_LOG(SG_GENERAL, SG_INFO, "Tesselating base poly : " << base_poly.Contours() << " contours " );
base_poly.Tesselate();
#if 0
#if 0
char desc[128];
sprintf( desc, "poly" );
tgShapefile::FromTriangles( base_poly, debug_root, "base_poly", desc );
@ -727,12 +727,12 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
triangulation_time = triangulation_end - triangulation_start;
#if 0
#if 0
// CheckZFighting( "post triangulation", debug_root, base_poly, rwy_polys, pvmt_polys );
if ( CheckZFightingTriangles( "post triangulation tris", debug_root, base_poly, rwy_polys, pvmt_polys ) ) {
char cmd[128];
sprintf( cmd, "echo %s >> zfight.txt\n", icao.c_str() );
system ( cmd );
snprintf(cmd, 128, "echo %s >> zfight.txt\n", icao.c_str());
system (cmd);
}
#endif
@ -791,19 +791,19 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
{
sgboTri.clear();
sgboTri.material = material;
for (int l = 0; l < 3; ++l) {
int index = nodes.add( poly.GetTriNode( i, l ) );
sgboTri.v_list.push_back( index );
// use 'the' normal
index = normals.add( vnt );
sgboTri.n_list.push_back( index );
index = texcoords.add( poly.GetTriTexCoord( i, l ) );
sgboTri.tc_list[0].push_back( index );
}
obj.add_triangle( sgboTri );
}
}
@ -820,19 +820,19 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
tgPolygon poly = pvmt_polys[k];
sgboTri.clear();
sgboTri.material = material;
for (int l = 0; l < 3; ++l) {
int index = nodes.add( poly.GetTriNode( i, l ) );
int index = nodes.add( poly.GetTriNode( i, l ) );
sgboTri.v_list.push_back( index );
// use 'the' normal
index = normals.add( vnt );
sgboTri.n_list.push_back( index );
index = texcoords.add( poly.GetTriTexCoord( i, l ) );
sgboTri.tc_list[0].push_back( index );
}
obj.add_triangle( sgboTri );
}
}
@ -849,42 +849,42 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
tgPolygon poly = line_polys[k];
sgboTri.clear();
sgboTri.material = material;
for (int l = 0; l < 3; ++l) {
int index = nodes.add( poly.GetTriNode( i, l ) );
int index = nodes.add( poly.GetTriNode( i, l ) );
sgboTri.v_list.push_back( index );
// use 'the' normal
index = normals.add( vnt );
sgboTri.n_list.push_back( index );
index = texcoords.add( poly.GetTriTexCoord( i, l ) );
sgboTri.tc_list[0].push_back( index );
}
obj.add_triangle( sgboTri );
}
}
// add base points
TG_LOG(SG_GENERAL, SG_INFO, "Adding base triangles");
TG_LOG(SG_GENERAL, SG_INFO, "Adding base triangles");
std::string material = "Grass";
for (unsigned int k = 0; k < base_poly.Triangles(); ++k) {
sgboTri.clear();
sgboTri.material = material;
for (int l = 0; l < 3; ++l) {
int index = nodes.add( base_poly.GetTriNode( k, l ) );
int index = nodes.add( base_poly.GetTriNode( k, l ) );
sgboTri.v_list.push_back( index );
// use 'the' normal
index = normals.add( vnt );
sgboTri.n_list.push_back( index );
index = texcoords.add( base_poly.GetTriTexCoord( k, l ) );
sgboTri.tc_list[0].push_back( index );
}
obj.add_triangle( sgboTri );
}
@ -1025,12 +1025,12 @@ void Airport::BuildBtg(const std::string& root, const string_list& elev_src )
{
sgboPt.clear();
sgboPt.material = rwy_lights[i].GetType();
for ( unsigned int j = 0; j < rwy_lights[i].ContourSize(); ++j )
{
int index = nodes.add( rwy_lights[i].GetPosition(j) );
sgboPt.v_list.push_back( index );
index = normals.add( rwy_lights[i].GetNormal(j) );
sgboPt.n_list.push_back( index );
}
@ -1177,31 +1177,31 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
char layer[128];
char desc[128];
sprintf( layer, "zfighting_%s", prefix );
snprintf(layer, 128, "zfighting_%s", prefix);
double min_area_thresh = 1.0e-10;
bool zfighting = false;
if ( rwy_polys.size() ) {
for ( unsigned int i = 0; i < rwy_polys.size(); ++i ) {
tgPolygon subject = rwy_polys[i];
for ( unsigned int j = 0; j < subject.Triangles(); ++j ) {
for ( unsigned int j = 0; j < subject.Triangles(); ++j ) {
tgTriangle subTri = subject.GetTriangle( j );
tgRectangle subBB = subTri.GetBoundingBox();
for ( unsigned int k = 0; k < pvmt_polys.size(); ++k ) {
tgPolygon test = pvmt_polys[k];
for ( unsigned int l = 0; l < test.Triangles(); ++l ) {
tgTriangle testTri = test.GetTriangle( l );
tgRectangle testBB = testTri.GetBoundingBox();
if ( subBB.intersects( testBB ) ) {
// find the intersection
tgPolygon intersection = tgTriangle::Intersect( subTri, testTri );
for ( unsigned int m = 0; m < intersection.Contours(); m++ ) {
tgContour intContour = intersection.GetContour( m );
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
TG_LOG( SG_GENERAL, SG_ALERT, prefix << "Z-FIGHTING between runway poly " << i << " and pavement poly " << k << " contour has " << intContour.GetSize() << " nodes " << " area is " << intContour.GetArea() );
sprintf( desc, "rwy_%06u_pvmt_%06u", i, j );
tgShapefile::FromContour( intContour, debug_root, layer, desc );
@ -1219,36 +1219,36 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
if ( pvmt_polys.size() ) {
for ( unsigned int i = 0; i < pvmt_polys.size(); ++i ) {
tgPolygon subject = pvmt_polys[i];
for ( unsigned int j = 0; j < subject.Triangles(); ++j ) {
for ( unsigned int j = 0; j < subject.Triangles(); ++j ) {
tgTriangle subTri = subject.GetTriangle( j );
tgRectangle subBB = subTri.GetBoundingBox();
for ( unsigned int k = 0; k < rwy_polys.size(); ++k ) {
tgPolygon test = rwy_polys[k];
for ( unsigned int l = 0; l < test.Triangles(); ++l ) {
tgTriangle testTri = test.GetTriangle( l );
tgRectangle testBB = testTri.GetBoundingBox();
if ( subBB.intersects( testBB ) ) {
// find the intersection
tgPolygon intersection = tgTriangle::Intersect( subTri, testTri );
for ( unsigned int m = 0; m < intersection.Contours(); m++ ) {
tgContour intContour = intersection.GetContour( m );
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
TG_LOG( SG_GENERAL, SG_ALERT, prefix << "Z-FIGHTING between pavement poly " << i << " and runway poly " << k << " contour has " << intContour.GetSize() << " nodes " << " area is " << intContour.GetArea() );
sprintf( desc, "pvmt_%06u_rwy_%06u", i, j );
tgShapefile::FromContour( intContour, debug_root, layer, desc );
zfighting = true;
}
}
}
}
}
}
}
}
}
// intersect base poly with each runway and pavement poly
tgPolygon subject = base_poly;
@ -1261,11 +1261,11 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
for ( unsigned int k = 0; k < test.Triangles(); ++k ) {
tgTriangle testTri = test.GetTriangle( k );
tgRectangle testBB = testTri.GetBoundingBox();
if ( subBB.intersects( testBB ) ) {
// find the intersection
tgPolygon intersection = tgTriangle::Intersect( subTri, testTri );
for ( unsigned int m = 0; m < intersection.Contours(); m++ ) {
tgContour intContour = intersection.GetContour( m );
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
@ -1274,7 +1274,7 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
tgShapefile::FromContour( intContour, debug_root, layer, desc );
zfighting = true;
}
}
}
}
}
}
@ -1289,14 +1289,14 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
for ( unsigned int k = 0; k < test.Triangles(); ++k ) {
tgTriangle testTri = test.GetTriangle( k );
tgRectangle testBB = testTri.GetBoundingBox();
if ( subBB.intersects( testBB ) ) {
// find the intersection
tgPolygon intersection = tgTriangle::Intersect( subTri, testTri );
for ( unsigned int m = 0; m < intersection.Contours(); m++ ) {
tgContour intContour = intersection.GetContour( m );
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
if ( (intContour.GetSize() > 2) && (intContour.GetArea() > min_area_thresh) ) {
TG_LOG( SG_GENERAL, SG_ALERT, prefix << "Z-FIGHTING between base poly and pavement poly " << j << " contour has " << intContour.GetSize() << " nodes " << " area is " << intContour.GetArea() );
sprintf( desc, "base_pvmt_%06u", j );
tgShapefile::FromContour( intContour, debug_root, layer, desc );
@ -1307,6 +1307,6 @@ bool Airport::CheckZFightingTriangles( const char* prefix, const char* debug_roo
}
}
}
return zfighting;
}

View file

@ -76,9 +76,10 @@ void ClosedPoly::AddNode( std::shared_ptr<BezNode> node )
feature_desc += "boundary";
}
TG_LOG(SG_GENERAL, SG_DEBUG, " Adding node " << node->GetLoc() << " to current linear feature " << cur_feature);
cur_feature = std::make_shared<LinearFeature>(feature_desc, 1.0f);
TG_LOG(SG_GENERAL, SG_DEBUG, " Adding node " << node->GetLoc() << " to current linear feature " << cur_feature);
}
cur_feature->AddNode( node );
}
}
@ -95,7 +96,8 @@ void ClosedPoly::CloseCurContour()
cur_feature->Finish(true, features.size() );
features.push_back(cur_feature);
cur_feature = NULL;
cur_feature.reset();
cur_feature = nullptr;
}
// add the contour to the poly - first one is the outer boundary
@ -428,12 +430,12 @@ int ClosedPoly::BuildBtg( tgpolygon_list& rwy_polys, tgcontour_list& slivers, tg
char layer[128];
if( shapefile_name.size() ) {
sprintf( layer, "%s_preclip", shapefile_name.c_str() );
tgShapefile::FromPolygon( pre_tess, "./airport_dbg", layer, std::string("preclip") );
snprintf(layer, 128, "%s_preclip", shapefile_name.c_str());
tgShapefile::FromPolygon(pre_tess, "./airport_dbg", layer, std::string("preclip"));
pre_tess.Tesselate();
sprintf( layer, "%s_preclip_tris", shapefile_name.c_str() );
tgShapefile::FromTriangles( pre_tess, "./airport_dbg", layer, std::string("preclip") );
snprintf(layer, 128, "%s_preclip_tris", shapefile_name.c_str());
tgShapefile::FromTriangles(pre_tess, "./airport_dbg", layer, std::string("preclip"));
//accum.ToShapefiles( "./airport_dbg", "accum", true );
}
@ -441,16 +443,16 @@ int ClosedPoly::BuildBtg( tgpolygon_list& rwy_polys, tgcontour_list& slivers, tg
tgPolygon clipped = accum.Diff( pre_tess );
if ( clipped.Contours() ) {
if( shapefile_name.size() ) {
sprintf( layer, "%s_postclip", shapefile_name.c_str() );
tgShapefile::FromPolygon( clipped, "./airport_dbg", layer, std::string("postclip") );
snprintf(layer, 128, "%s_postclip", shapefile_name.c_str());
tgShapefile::FromPolygon(clipped, "./airport_dbg", layer, std::string("postclip"));
}
// tgPolygon::RemoveSlivers( clipped, slivers );
if( shapefile_name.size() ) {
clipped.Tesselate();
sprintf( layer, "%s_postclip_tris", shapefile_name.c_str() );
tgShapefile::FromTriangles( clipped, "./airport_dbg", layer, std::string("postclip") );
snprintf(layer, 128, "%s_postclip_tris", shapefile_name.c_str());
tgShapefile::FromTriangles(clipped, "./airport_dbg", layer, std::string("postclip"));
}
clipped.SetMaterial( GetMaterial( surface_type ) );

View file

@ -11,7 +11,9 @@ std::string DebugTimeToString(time_t& tt)
{
char buf[256];
strcpy(buf,ctime(&tt));
strncpy(buf, ctime(&tt), 255);
buf[255] = '\0';
buf[strlen(buf)-1]='\0';
return std::string( buf );
}

File diff suppressed because it is too large Load diff

View file

@ -177,11 +177,11 @@ void tgAccumulator::ToShapefiles( const std::string& path, const std::string& la
if ( individual ) {
for (unsigned int i=0; i < accum.size(); i++) {
char layer[32];
sprintf( layer, "%s_%u", layer_prefix.c_str(), i );
snprintf(layer, 32, "%s_%u", layer_prefix.c_str(), i);
char shapefile[32];
sprintf( shapefile, "accum_%u", i );
tgShapefile::FromClipper( accum[i], path, layer, std::string(shapefile) );
snprintf(shapefile, 32, "accum_%u", i);
tgShapefile::FromClipper(accum[i], path, layer, std::string(shapefile));
}
} else {
ClipperLib::Paths clipper_result;
@ -209,7 +209,7 @@ void tgAccumulator::ToClipperfiles( const std::string& path, const std::string&
if ( individual ) {
char filename[256];
for (unsigned int i=0; i < accum.size(); i++) {
sprintf( filename, "%s/%s_%u", path.c_str(), layer_prefix.c_str(), i );
snprintf(filename, 256, "%s/%s_%u", path.c_str(), layer_prefix.c_str(), i);
file.open (filename);
file << accum[i];
@ -226,8 +226,8 @@ void tgAccumulator::ToClipperfiles( const std::string& path, const std::string&
if ( c.Execute( ClipperLib::ClipType::Union, clipper_result, ClipperLib::PolyFillType::NonZero, ClipperLib::PolyFillType::NonZero) ) {
char filename[256];
sprintf( filename, "%s/%s", path.c_str(), layer_prefix.c_str() );
snprintf(filename, 256, "%s/%s", path.c_str(), layer_prefix.c_str());
file.open (filename);
file << clipper_result;
file.close();

View file

@ -208,10 +208,10 @@ void tgChopper::Save(bool DebugShapefiles)
SGBucket b((*it).first);
tgpolygon_list const& polys = (*it).second;
sprintf(ds_name, "./bucket_%s", b.gen_index_str().c_str());
snprintf(ds_name, 64, "./bucket_%s", b.gen_index_str().c_str());
std::string path = root_path + "/" + b.gen_base_path();
sprintf(tile_name, "%ld", b.gen_index());
snprintf(tile_name, 16, "%ld", b.gen_index());
std::string polyfile = path + "/" + tile_name;
@ -220,7 +220,7 @@ void tgChopper::Save(bool DebugShapefiles)
uint32_t poly_index = GenerateIndex(path);
char poly_ext[32];
sprintf(poly_ext, "%u%.25s", poly_index, extra_extension.c_str());
snprintf(poly_ext, 32, "%u%.25s", poly_index, extra_extension.c_str());
polyfile = polyfile + "." + poly_ext;
gzFile fp;
@ -235,7 +235,7 @@ void tgChopper::Save(bool DebugShapefiles)
polys[i].SaveToGzFile(fp);
if (DebugShapefiles) {
sprintf(layer, "poly_%s-%u", b.gen_index_str().c_str(), i);
snprintf(layer, 32, "poly_%s-%u", b.gen_index_str().c_str(), i);
tgShapefile::FromPolygon(polys[i], ds_name, layer, "poly");
}
}

View file

@ -416,9 +416,9 @@ void tgPolygon::ToClipperFile( const tgPolygon& subject, const std::string& path
ClipperLib::Paths clipper_subject = tgPolygon::ToClipper( subject );
std::ofstream dmpfile;
char pathname[256];
sprintf( pathname, "%s/%s", path.c_str(), filename.c_str() );
dmpfile.open (pathname);
snprintf(pathname, 256, "%s/%s", path.c_str(), filename.c_str());
dmpfile.open(pathname);
dmpfile << clipper_subject;
dmpfile.close();
}