fix off by one in call to symbolize, and avoid nulls

This commit is contained in:
Alex Groce
2019-01-01 18:59:25 -07:00
parent 31460e7f0d
commit cc2925ffd2
3 changed files with 33 additions and 4 deletions

View File

@@ -48,7 +48,7 @@ void printBytes(const char* bytes) {
#define MAX_STR_LEN 3
TEST(Runlength, EncodeDecode) {
char* original = DeepState_CStr(MAX_STR_LEN);
char* original = DeepState_CStrUpToLen(MAX_STR_LEN);
char* encoded = encode(original);
char* roundtrip = decode(encoded);
if (!(strncmp(roundtrip, original, MAX_STR_LEN) == 0)) {

View File

@@ -159,6 +159,9 @@ extern int DeepState_ZeroSink(int);
/* Symbolize the data in the exclusive range `[begin, end)`. */
extern void DeepState_SymbolizeData(void *begin, void *end);
/* Symbolize the data in the exclusive range `[begin, end)` with no nulls. */
extern void DeepState_SymbolizeDataNoNull(void *begin, void *end);
/* Concretize some data in exclusive the range `[begin, end)`. Returns a
* concrete pointer to the beginning of the concretized data. */
extern void *DeepState_ConcretizeData(void *begin, void *end);

View File

@@ -170,6 +170,32 @@ void DeepState_SymbolizeData(void *begin, void *end) {
}
}
/* Symbolize the data in the exclusive range `[begin, end)`. */
void DeepState_SymbolizeDataNoNull(void *begin, void *end) {
uintptr_t begin_addr = (uintptr_t) begin;
uintptr_t end_addr = (uintptr_t) end;
if (begin_addr > end_addr) {
DeepState_Abandon("Invalid data bounds for DeepState_SymbolizeData");
} else if (begin_addr == end_addr) {
return;
} else {
uint8_t *bytes = (uint8_t *) begin;
for (uintptr_t i = 0, max_i = (end_addr - begin_addr); i < max_i; ++i) {
if (DeepState_InputIndex >= DeepState_InputSize) {
DeepState_Abandon("Read too many symbols");
}
if (FLAGS_verbose_reads) {
printf("Reading byte at %u\n", DeepState_InputIndex);
}
bytes[i] = DeepState_Input[DeepState_InputIndex++];
if (bytes[i] == 0) {
bytes[i] = 1;
}
}
}
}
/* Concretize some data in exclusive the range `[begin, end)`. */
void *DeepState_ConcretizeData(void *begin, void *end) {
return begin;
@@ -185,7 +211,7 @@ void DeepState_AssignCStr_C(char* str, size_t len, const char* allowed) {
}
if (len) {
if (!allowed) {
DeepState_SymbolizeData(str, &(str[len - 1]));
DeepState_SymbolizeDataNoNull(str, &(str[len]));
} else {
uint32_t allowed_size = strlen(allowed);
for (int i = 0; i < len; i++) {
@@ -208,7 +234,7 @@ char *DeepState_CStr_C(size_t len, const char* allowed) {
DeepState_GeneratedStrings[DeepState_GeneratedStringsIndex++] = str;
if (len) {
if (!allowed) {
DeepState_SymbolizeData(str, &(str[len - 1]));
DeepState_SymbolizeDataNoNull(str, &(str[len]));
} else {
uint32_t allowed_size = strlen(allowed);
for (int i = 0; i < len; i++) {
@@ -224,7 +250,7 @@ char *DeepState_CStr_C(size_t len, const char* allowed) {
void DeepState_SymbolizeCStr_C(char *begin, const char* allowed) {
if (begin && begin[0]) {
if (!allowed) {
DeepState_SymbolizeData(begin, begin + strlen(begin));
DeepState_SymbolizeDataNoNull(begin, begin + strlen(begin));
} else {
uint32_t allowed_size = strlen(allowed);
uint8_t *bytes = (uint8_t *) begin;