Skip to content

Commit d717d8a

Browse files
committed
Fixed issue #13: bin2cpp crashes when encoding a generated file with size=1000 fill=random seed=1
1 parent ba1d2dc commit d717d8a

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

msvc/bin2cpp_unittest.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<ClCompile Include="..\test\main.cpp" />
108108
<ClCompile Include="..\test\TestExtraction.cpp" />
109109
<ClCompile Include="generated_files\testHtml100000\_testHtml100000.cpp" />
110+
<ClCompile Include="generated_files\testIssue13\_testIssue13.cpp" />
110111
<ClCompile Include="generated_files\testRandom1\_testRandom1.cpp" />
111112
<ClCompile Include="generated_files\testRandom2\_testRandom2.cpp" />
112113
<ClCompile Include="generated_files\testRandom3\_testRandom3.cpp" />
@@ -133,6 +134,7 @@
133134
<ClInclude Include="..\test\gtesthelper.h" />
134135
<ClInclude Include="..\test\TestExtraction.h" />
135136
<ClInclude Include="generated_files\testHtml100000\_testHtml100000.h" />
137+
<ClInclude Include="generated_files\testIssue13\_testIssue13.h" />
136138
<ClInclude Include="generated_files\testRandom1\_testRandom1.h" />
137139
<ClInclude Include="generated_files\testRandom2\_testRandom2.h" />
138140
<ClInclude Include="generated_files\testRandom3\_testRandom3.h" />

msvc/bin2cpp_unittest.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
<ClCompile Include="generated_files\testRandom3\_testRandom3.cpp">
5858
<Filter>Generated Files</Filter>
5959
</ClCompile>
60+
<ClCompile Include="generated_files\testIssue13\_testIssue13.cpp">
61+
<Filter>Generated Files</Filter>
62+
</ClCompile>
6063
</ItemGroup>
6164
<ItemGroup>
6265
<None Include="bin2cppTest.x86.debug.xml">
@@ -98,6 +101,9 @@
98101
<ClInclude Include="generated_files\testRandom3\_testRandom3.h">
99102
<Filter>Generated Files</Filter>
100103
</ClInclude>
104+
<ClInclude Include="generated_files\testIssue13\_testIssue13.h">
105+
<Filter>Generated Files</Filter>
106+
</ClInclude>
101107
</ItemGroup>
102108
<ItemGroup>
103109
<CustomBuild Include="generate_test_files.bat" />

msvc/generate_test_files.bat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ REM mkdir %OUTDIR% 1>NUL 2>NUL
6060
REM testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=31457280 --fill=sequential
6161
REM bin2cpp.exe %OUTDIR%\%TEST_NAME%.bin %OUTDIR% _%TEST_NAME%.h %TEST_NAME% 450 -override
6262

63+
set TEST_NAME=testIssue13
64+
set OUTDIR=.\generated_files\%TEST_NAME%
65+
mkdir %OUTDIR% 1>NUL 2>NUL
66+
echo testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=1000 --fill=random --seed=1
67+
testfilegenerator.exe --file=%OUTDIR%\%TEST_NAME%.bin --size=1000 --fill=random --seed=1
68+
echo bin2cpp.exe %OUTDIR%\%TEST_NAME%.bin %OUTDIR% _%TEST_NAME%.h %TEST_NAME% 100 -override
69+
bin2cpp.exe %OUTDIR%\%TEST_NAME%.bin %OUTDIR% _%TEST_NAME%.h %TEST_NAME% 100 -override
70+
6371
dir >NUL

src/bin2cpp.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,17 @@ namespace bin2cpp
448448
//read a chunk of the file
449449
size_t readSize = fread(buffer, 1, iChunkSize, input);
450450

451-
//send to MD5 for analysist
452-
MD5Update(&context, buffer, readSize);
453-
454451
bool isLastChunk = !(readSize == iChunkSize);
455452

456-
if (isLastChunk)
453+
if (isLastChunk || readSize == 0)
457454
fprintf(cpp, " oLength = %d;\n", lastSegmentSize);
458455

456+
if (readSize == 0)
457+
continue; //nothing to output if nothing was read
458+
459+
//send to MD5 for analysist
460+
MD5Update(&context, buffer, readSize);
461+
459462
//output mode 1
460463
fprintf(cpp, " buffer = \"%s\"; if (iIndex == index) return buffer; index++;\n", toCppString(buffer, readSize).c_str(), readSize);
461464

test/TestExtraction.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "generated_files\testText10\_testText10.h"
1010
#include "generated_files\testText1000\_testText1000.h"
1111
#include "generated_files\testText100000\_testText100000.h"
12+
#include "generated_files\testIssue13\_testIssue13.h"
1213

1314
gTestHelper & hlp = gTestHelper::getInstance();
1415

@@ -161,3 +162,18 @@ TEST_F(TestExtraction, testSequential10MB)
161162
bool equal = hlp.isFileEquals(expectedFilePath.c_str(), outputFilePath.c_str(), reason);
162163
ASSERT_TRUE(equal) << reason.c_str();
163164
}
165+
166+
TEST_F(TestExtraction, testIssue13)
167+
{
168+
static const std::string expectedFilePath = getExpectedFilePath();
169+
static const std::string outputFilePath = getActualFilePath();
170+
171+
const bin2cpp::File & file = bin2cpp::getTestIssue13File();
172+
bool extractSuccess = file.save(outputFilePath.c_str());
173+
ASSERT_TRUE(extractSuccess);
174+
175+
//assert content is the same
176+
std::string reason;
177+
bool equal = hlp.isFileEquals(expectedFilePath.c_str(), outputFilePath.c_str(), reason);
178+
ASSERT_TRUE(equal) << reason.c_str();
179+
}

0 commit comments

Comments
 (0)