Skip to content

Commit ed23539

Browse files
Fix bug #10. json stamp crashed on exctracting string, when there were enough data for oracle, but not enough data for anything else. It have been choosing from emply list. This leaded to crash. Some tests also added
1 parent 2b5ad94 commit ed23539

File tree

4 files changed

+92
-22
lines changed

4 files changed

+92
-22
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ clean: blob-stamper-clean
4343
$(MAKE) -C t clean
4444
$(MAKE) -C examples clean
4545
$(MAKE) -C libtappp clean
46+
$(MAKE) -C console_demo clean
4647
@echo Clean done!
4748

4849
test:

blobstamper/stamp_json.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ PoolPickerStamp::ExtractStr(std::shared_ptr<Blob> blob)
7777
if (unbounded_pool.size()>0)
7878
target_pool = unbounded_pool;
7979

80+
if (target_pool.size() == 0)
81+
{
82+
/* Most probably we are in out of data situation. Check it
83+
* and throw an exception. Or abort if something goes wrong
84+
*
85+
* Normally caller should not call this method when he is out of data
86+
* so it is OK to do the check at the end, when get no success, and not
87+
* in the beginning.
88+
*/
89+
if (blob->Size() < this->minSize())
90+
{
91+
throw OutOfData();
92+
} else
93+
{
94+
fprintf(stderr, "Something is really wrong in PoolPickerStamp::ExtractStr\n");
95+
abort();
96+
}
97+
}
98+
8099
size_t index = OracleProportion(oracle, 0, target_pool.size() - 1);
81100
return target_pool[index].lock()->ExtractStr(blob);
82101
}

t/321-galley-recursion-experiments_2.cpp

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
#include <string>
2020
#include <vector>
21+
#include <cstring>
22+
23+
2124
#define WANT_TEST_EXTRAS
2225
#include <tap++/tap++.h>
2326

2427
#include "blobstamper/stamp_json.h"
25-
26-
// tmp
27-
#include <fstream>
28+
#include "blobstamper/oracle.h"
2829

2930
using namespace TAP;
3031

@@ -36,30 +37,78 @@ unsigned char bin_sample[]= {49, 22, 152, 226, 89, 119, 247, 115, 43, 42, 243, 7
3637
int
3738
main()
3839
{
39-
std::fstream f{"/dev/random"};
40-
// std::fstream f{"buf"};
41-
if (!f)
42-
std::cerr << "Unable to open file";
43-
44-
std::vector<char> buffer (128,0);
45-
f.read(&buffer[0], buffer.size());
46-
47-
// auto blob = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
48-
auto blob = std::make_shared<Blob>(&buffer[0], buffer.size());
49-
50-
51-
auto stamp_j = std::make_shared<StampJSON>();
52-
53-
std::string s = stamp_j->ExtractStr(blob);
5440

55-
printf("%s\n", s.c_str());
41+
// If you uncomment this it will crash for reason I do not understand
5642

57-
/*
43+
/* auto blob1 = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
44+
auto stamp_j1 = std::make_shared<StampJSON>();
45+
std::string s1 = stamp_j1->ExtractStr(blob1);
46+
printf("%s\n", s1.c_str());
47+
*/
5848

59-
TEST_START(6);
49+
TEST_START(5);
6050
{
51+
auto blob = std::make_shared<Blob>((char *)bin_sample, strlen((char *)bin_sample));
52+
auto stamp_j = std::make_shared<StampJSON>();
6153

54+
std::string s = stamp_j->ExtractStr(blob);
55+
std::string expected = "[6850509402014839822, 3438255817691106319, \"resulted\", -2.684757453484730872673776686700739040682020879678934003695336094670709694419006239953740106372555463816175000634871124431422513570790242370232656601974908085084433064181237341375458199532539671263821862377193715039215799333468172751725903574125176464661297323397544866180407166347114750393787405005704461598334438005517341732432719884411807209593715412099886215291366653445948031730949878692626953125e-151, \"lushes\", -5199661808640109653, \"denomination\", -3.06532686549173138956803086330182850360870361328125, \"sedating\", \"robots\", -3.3696970613711244822470283884492113423278459359891097388748050077510095184860700081976239055565263066452656899596451722800338757224380970001220703125e-42, \"preface\", 9.55422367779008699072920786882312943145717681740324270561686674939552239415695463827122863940711666092367434557202401330797838517640477979352142678948212051182053983211517333984375e-56, 6300155112918555921, \"dams\", 2.411323842459936645124797826433320804619144161893916130561265623523643502350374626190490408739886564139366504029821786107467040377895714047017891228899122281601579488797245463600235576598531485903332393648607370715931713232242018751523644727805408352870771308312443806176048554264200149458533769648133943422986030732712523236966445578809656378067940216124429747414289524093606877704785435824362397944294729373146388523502884316004124548536154374326842524560415029933595880452289294570895862703927051929788804023836835074268010108198185051300394774230223405817676130206688777308144339841912135245325959771275736867526109676372404347501558868228887456576678950716422841427035057837446885287135955877602100372314453125e-287, 9188031415654983422, -5901895462997796268, \"blazer\"]";
56+
is(s, expected, "json stamp works well");
57+
}
58+
/* Smallest sample giving sane output */
59+
{
60+
auto blob = std::make_shared<Blob>((char *)bin_sample, 10); /* ORACLE_SIZE = 2 + int = 8 */
61+
auto stamp_j = std::make_shared<StampJSON>();
6262

63+
std::string s = stamp_j->ExtractStr(blob);
64+
std::string expected = "3038649880288027288";
65+
is(s, expected, "json stamp on shortest data works well");
66+
}
67+
/* Check throw OutOfMemory when data length is less then ORACLE_SIZE */
68+
{
69+
auto blob = std::make_shared<Blob>((char *)bin_sample, ORACLE_SIZE - 1);
70+
auto stamp_j = std::make_shared<StampJSON>();
71+
72+
int cought = 0;
73+
try{
74+
std::string s = stamp_j->ExtractStr(blob);
75+
}
76+
catch (OutOfData)
77+
{
78+
cought = 1;
79+
}
80+
ok(cought, "Throws OutOfData if have data size less then ORACLE_SIZE");
81+
}
82+
/* Check throw OutOfMemory when data length is equal to ORACLE_SIZE */
83+
{
84+
auto blob = std::make_shared<Blob>((char *)bin_sample, ORACLE_SIZE);
85+
auto stamp_j = std::make_shared<StampJSON>();
86+
87+
int cought = 0;
88+
try{
89+
std::string s = stamp_j->ExtractStr(blob);
90+
}
91+
catch (OutOfData)
92+
{
93+
cought = 1;
94+
}
95+
ok(cought, "Throws OutOfData if have data size equal to ORACLE_SIZE");
96+
}
97+
/* Check throw OutOfMemory when data length is bit bigger than ORACLE_SIZE */
98+
/* But not big enough to give any output */
99+
{
100+
auto blob = std::make_shared<Blob>((char *)bin_sample, ORACLE_SIZE + 1);
101+
auto stamp_j = std::make_shared<StampJSON>();
102+
103+
int cought = 0;
104+
try{
105+
std::string s = stamp_j->ExtractStr(blob);
106+
}
107+
catch (OutOfData)
108+
{
109+
cought = 1;
110+
}
111+
ok(cought, "Throws OutOfData if have data size is a bit bigger then ORACLE_SIZE");
63112
}
64-
TEST_END;*/
113+
TEST_END;
65114
}

test_with_sanitizers.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export LDFLAGS="-g -fsanitize=address -fsanitize=undefined -fno-sanitize-recove
99
make clean
1010
make
1111
make examples
12+
make -C console_demo
1213
make test

0 commit comments

Comments
 (0)