8000 Hide blob's attributes into protected area, fix bug when not all blob… · postgrespro/libblobstamper@de236fd · GitHub
[go: up one dir, main page]

Skip to content

Commit de236fd

Browse files
Hide blob's attributes into protected area, fix bug when not all blob data where shifted, add test case for such bug
1 parent df7987e commit de236fd

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

blobstamper/blob.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Blob::Dump()
3333
Blob
3434
Blob::ShiftBytes(size_t n)
3535
{
36-
if (begin + n > end)
36+
if (begin + n - 1 > end)
3737
{
3838
Blob empty(NULL, -1);
3939
return empty; /* not enough data */
@@ -49,6 +49,12 @@ Blob::ShiftBytes(size_t n)
4949
return new_blob;
5050
}
5151

52+
size_t
53+
Blob::Size()
54+
{
55+
return end - begin + 1;
56+
}
57+
5258
void *
5359
Blob::ShiftSingleStampBin(StampGeneric& stmp)
5460
{
@@ -64,8 +70,9 @@ Blob::ShiftSingleStampStr(StampGeneric& stmp)
6470
void
6571
Blob::DataDup(char *& data_out, size_t& size_out)
6672
{
67-
size_out = end - begin + 1;
68-
data_out = (char *)malloc(size);
73+
size_out = Size();
74+
data_out = (char *)malloc(size_out);
75+
//FIXME add out of memory check here!!!!
6976
memcpy(data_out, data + begin, size_out);
7077
}
7178

blobstamper/blob.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ class StampGeneric;
88

99
class Blob
1010
{
11-
private:
11+
protected:
12+
char* data;
13+
int size;
14+
int begin;
15+
int end;
1216
public:
1317
Blob(char * data, int size);
1418
bool isEmpty ();
19+
size_t Size();
1520
void Dump();
1621
Blob ShiftBytes(size_t n);
1722
void DataDup(char *& data_out, size_t& size_out);
1823

1924
void * ShiftSingleStampBin(StampGeneric &stmp);
2025
std::string ShiftSingleStampStr(StampGeneric &stmp);
21-
22-
char* data; /*FIXME потом сделать private*/
23-
int size;
24-
int begin;
25-
int end;
2626
};
2727

2828
#endif /*BLOB_H*/

blobstamper/stamp.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ StampGeneric::Extract(Blob &blob)
2222

2323
if (blob2.isEmpty()) /* original blob does not have enought data */
2424
return NULL;
25-
void *res = malloc(min_size);
26-
if(! res)
27-
{
28-
fprintf(stderr, "Out of memory\n");
29-
return NULL;
30-
}
31-
memcpy(res, blob2.data + blob2.begin, min_size);
32-
return res;
25+
26+
size_t res_size;
27+
char *res;
28+
blob2.DataDup(res,res_size);
29+
return (void *) res;
3330
}
3431

3532

t/100-stamp-base.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ using namespace TAP;
2020

2121
/* This stamps chops first two bytes and treat them as string */
2222
/* Never do this in real live, as blob is binary and may have \0 in the middle of it*/
23-
class StampForTest: public StampGeneric
23+
class StampTwoChars: public StampGeneric
2424
{
2525
public:
26-
StampForTest();
26+
StampTwoChars();
2727
std::string ExtractStr(Blob &blob) override;
2828
};
2929

30-
StampForTest::StampForTest() : StampGeneric()
30+
StampTwoChars::StampTwoChars() : StampGeneric()
3131
{
3232
min_size = 2; /* This stamp shifts two characters only */
3333
max_size = 2;
3434
is_fixed_size = true;
3535
}
3636

3737
std::string
38-
StampForTest::ExtractStr(Blob &blob)
38+
StampTwoChars::ExtractStr(Blob &blob)
3939
{
4040
char * buf;
4141
size_t buf_size;
@@ -66,16 +66,16 @@ main()
6666
char *ptr;
6767
size_t size;
6868

69-
TEST_START(7);
69+
TEST_START(8);
7070

71-
/* Test that ShiftSingleStampStr shifts ok with StampForTest stamp */
71+
/* Test that ShiftSingleStampStr shifts ok with StampTwoChars stamp */
7272
{ /* 1..3 */
7373
std::string expected1 = "12";
7474
char* expected2 = "34567";
7575

7676

7777
Blob blob(short_sample, strlen(short_sample));
78-
StampForTest stamp;
78+
StampTwoChars stamp;
7979
std::string str = blob.ShiftSingleStampStr(stamp);
8080
ok(str == expected1, "ShiftSingleStampStr: shifts ok");
8181

@@ -91,7 +91,7 @@ main()
9191
std::string expected2 = "34";
9292
std::string expected3 = "56";
9393

94-
StampForTest stamp;
94+
StampTwoChars stamp;
9595
StampList stamp_list(stamp);
9696
Blob blob(short_sample, strlen(short_sample));
9797
std::list<std::string> res = stamp_list.ExtractStrList(blob);
@@ -112,6 +112,16 @@ main()
112112

113113
ok(res.empty(), "ExtractStrList: The rest of the list is empty");
114114
}
115+
/* Chekc that data is shifted till blob data is empty*/
116+
{ /* 8 */
117+
char sample_two_bytes[]="12";
118+
std::string expected1 = "12";
119+
Blob blob(sample_two_bytes, strlen(sample_two_bytes));
120+
StampTwoChars stamp;
121+
std::string str = blob.ShiftSingleStampStr(stamp);
122+
ok(str == expected1, "ShiftSingleStampStr: shifts last two bytes ok");
123+
124+
}
115125

116126
TEST_END;
117127
}

0 commit comments

Comments
 (0)
0