HydrologicEngineeringCenter / hec-dss

source code for HEC-DSS (Data Storage System)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stack smashing detected

ktarbet opened this issue · comments

OS: Linux/Ubuntu

JRE: openjdk 8

package hec.heclib.dss;

import hec.heclib.TestFiles;
import hec.heclib.util.Heclib;
import org.junit.jupiter.api.Test;
import hec.heclib.dss.*;

public class StackSmashTest

{    @Test public void test_stack_smash_detected() throws Exception {
 smash(); 
 }
 private static void smash() throws Exception{ // 

HecDSSFileAccess.zsetMessageLevel(HecDataManager.MESS_METHOD_GLOBAL, HecDataManager.MESS_LEVEL_USER_DIAG); 

HecDSSFileAccess.zsetMessageLevel(HecDataManager.MESS_METHOD_GLOBAL, HecDataManager.MESS_LEVEL_INTERNAL_DIAG_2);
 String destFileName = TestFiles.getFile("output_dss_stack_smash.dss",false);
HecDSSUtilities u = new HecDSSUtilities(); 
u.setDSSFileName(destFileName,6);
u.open();  
HecDSSUtilities f = new HecDSSUtilities(); 
String fileInput = TestFiles.getFile("containsGrids7.dss",true); // existing file 
f.setDSSFileName(fileInput); f.copyFile(destFileName); 
u.close();  
}
}

I'm also reproducing a crash on CentOs 7.9.2009

Stack: [0x00007f7c45f14000,0x00007f7c46015000], sp=0x00007f7c4600f7b8, free space=1005k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [libjavaHeclib.so+0x1d9304] zmessageDebug+0x213

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j hec.heclib.util.Heclib.Hec_zgridRetrieve([ILhec/heclib/grid/GridStructContainer;Z)I+0
j hec.heclib.util.Heclib.zgridRetrieve([ILhec/heclib/grid/GridStructContainer;Z)I+13
j hec.heclib.grid.GriddedData.readStructGrids(Lhec/heclib/grid/GridStructContainer;Z[I)I+109
j hec.heclib.grid.GriddedData.retrieveGriddedContainer(ZLhec/heclib/grid/GridData;[I)Lhec/heclib/grid/GridInfo;+150
j hec.heclib.grid.GriddedData.retrieveGriddedData(ZLhec/heclib/grid/GridData;[I)Lhec/heclib/grid/GridInfo;+23
j hec.heclib.dss.HecDSSUtilities.copyGrid(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I+44
j hec.heclib.dss.HecDSSUtilities.copyRecords(Lhec/heclib/dss/HecDataManager;Lhec/heclib/dss/HecDataManager;Ljava/util/Vector;Ljava/util/Vector;)I+105
j hec.heclib.dss.HecDSSUtilities.copyRecords(Lhec/heclib/dss/HecDataManager;Lhec/heclib/dss/HecDataManager;Ljava/util/Vector;)I+5
j hec.heclib.dss.HecDSSUtilities.copyRecordsFrom(Lhec/heclib/dss/HecDataManager;Ljava/util/Vector;)I+4
j hec.heclib.dss.HecDSSUtilities.copyFile(Lhec/heclib/dss/HecDataManager;)I+55
j hec.heclib.dss.HecDSSUtilities.copyFile(Ljava/lang/String;)I+92
j hec.heclib.dss.BugTests.smash()V+58
j hec.heclib.dss.BugTests.test_stack_smash_detected()V+0

commenting out the call to zmessageDebug() makes the crash go away.
The strings being passed , message2=_srsDefinition = 'PROJCS["USA_Contiguous_Albers_Equal_Area_Conic_USGS_version",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Albers"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-96.0],PARAMETER["Standard_Parallel_1",29.5],PARAMETER["Standard_Parallel_2",45.5],PARAMETER["Latitude_Of_Origin",23.0],UNIT["Meter",1.0]]'

strlen(_srsDefinition) = 480

message1 =="_srsDefinition string read: "

void zmessageDebug(long long *ifltab, int functionID, const char *message1, const char *message2)
{
	//  ASCII only!  For porting installation only - does not follow Unicode standards...

	char dmess[20] = " =====DSS===Debug: ";
	char semicolon[2] = ";";
	char blank[2] = " ";
	char mess[500];

The note says ASCII only.. hum... That mess[500] is not big enough, but making that larger (or allocate) doesn't seem to be the full solution.

The Linux version of stringCat has the issue. I'll test a proposed fix.

int stringCat (char *destination, size_t sizeOfDestination, const char* source, size_t lenSource)