new string functions

This commit is contained in:
Alex Groce
2018-12-31 08:02:09 -07:00
parent dee5dd549c
commit 58a1d5120d
3 changed files with 30 additions and 67 deletions

View File

@@ -139,19 +139,6 @@ DEEPSTATE_INLINE static int8_t DeepState_MaxChar(int8_t v) {
return (int8_t) DeepState_MaxInt(v);
}
/* Given a char pointer (assumed to have enough storage), places a string
* value in that location, with size characters (before null). If allowed is
* non-null, chooses characters from allowed char array. If null_terminated
* is true, the string will be null-terminated at size+1, and no prior character
* will be null. For null-terminated strings, storage must have space for the
* null terminator, so size should be 1 less than actual size. */
extern void DeepState_AssignString(char *dest, size_t size, const char* allowed,
size_t allowed_size, int null_terminated);
/* Returns a null-terminated string of size characters (before null), allocated on
* heap. DeepState will handle freeing these strings at termination of the test. */
extern char* DeepState_String(size_t size);
/* Function to clean up generated strings, and any other DeepState-managed data. */
extern void DeepState_CleanUp();
@@ -176,6 +163,11 @@ extern void DeepState_SymbolizeData(void *begin, void *end);
* concrete pointer to the beginning of the concretized data. */
extern void *DeepState_ConcretizeData(void *begin, void *end);
/* Assign a symbolic C string of length `len` with only chars in allowed,
* if allowed is non-null */
extern void DeepState_AssignCStr(char* str, size_t len, const char* allowed,
size_t allowed_size);
/* Return a symbolic C string of length `len`. */
extern char *DeepState_CStr(size_t len);

View File

@@ -471,23 +471,17 @@ struct Comparer {
}
};
/* Given a char pointer (assumed to have enough storage), places a C string
* value in that location, with up to max_size characters (strlen, not counting
* null). If allowed is non-null, chooses characters from allowed char array.
* If null_terminated is true, the string will be null-terminated at size+1, and
* no prior character will be null. For null-terminated strings, storage must have
* space for the null terminator, so max_size should be 1 less than actual size. */
inline static void DeepState_AssignCString(char *dest, size_t max_size, const char* allowed,
size_t allowed_size, int null_terminated) {
uint32_t size = DeepState_UIntInRange(0, max_size);
DeepState_AssignString(dest, Pump(size, max_size), allowed, allowed_size, null_terminated);
/* Like DeepState_AssignCStr, but Pumps through possible string sizes. */
inline static void DeepState_AssignCStrMax(char* str, size_t max_len, const char* allowed,
size_t allowed_size) {
uint32_t size = DeepState_UIntInRange(0, max_len);
DeepState_AssignCStr(str, Pump(size, max_len), allowed, allowed_size);
}
/* Returns a null-terminated C string of up to max_size characters (strlen), allocated on
* heap. DeepState will handle freeing these strings at termination of the test. */
inline static char* DeepState_CString(size_t max_size) {
uint32_t size = DeepState_UIntInRange(0, max_size);
return DeepState_String(Pump(size, max_size));
/* Like DeepState_CStr, but Pumps through possible string sizes. */
inline static char* DeepState_CStrMax(size_t max_len) {
uint32_t size = DeepState_UIntInRange(0, max_len);
return DeepState_CStr(Pump(size, max_len));
}
} // namespace deepstate

View File

@@ -175,6 +175,21 @@ void *DeepState_ConcretizeData(void *begin, void *end) {
return begin;
}
/* Assign a symbolic C string of length `len`. */
void DeepState_AssignCStr(char* str, size_t len, const char* allowed,
size_t allowed_size) {
if (SIZE_MAX == len) {
DeepState_Abandon("Can't create an SIZE_MAX-length string.");
}
if (NULL == str) {
DeepState_Abandon("Attempted to populate null pointer.");
}
for (int i = 0; i < len; i++) {
str[i] = allowed[DeepState_UIntInRange(0, allowed_size)];
}
str[len] = '\0';
}
/* Return a symbolic C string of length `len`. */
char *DeepState_CStr(size_t len) {
if (SIZE_MAX == len) {
@@ -184,6 +199,7 @@ char *DeepState_CStr(size_t len) {
if (NULL == str) {
DeepState_Abandon("Can't allocate memory.");
}
DeepState_GeneratedStrings[DeepState_GeneratedStringsIndex++] = str;
if (len) {
DeepState_SymbolizeData(str, &(str[len - 1]));
}
@@ -316,45 +332,6 @@ int32_t DeepState_MaxInt(int32_t v) {
0x80000000U);
}
/* Given a char pointer (assumed to have enough storage), places a string
* value in that location, with size characters (before null). If allowed is
* non-null, chooses characters from allowed char array. If null_terminated
* is true, the string will be null-terminated at size+1, and no prior character
* will be null. For null-terminated strings, storage must have space for the
* null terminator, so size should be 1 less than actual size. */
void DeepState_AssignString(char *dest, size_t size, const char* allowed,
size_t allowed_size, int null_terminated) {
for(int i = 0; i < size; i++) {
if (allowed) {
size_t index = DeepState_UIntInRange(0, allowed_size);
dest[i] = allowed[index];
} else if (null_terminated) {
dest[i] = DeepState_UCharInRange(1, UCHAR_MAX); /* Disallow null termination till end. */
} else {
dest[i] = DeepState_UChar();
}
}
if (null_terminated) {
dest[size] = 0;
}
}
/* Returns a null-terminated string of size characters (before null), allocated on
* heap. DeepState will handle freeing these strings at termination of the test. */
char* DeepState_String(size_t size) {
char* rstr = malloc(size+1);
if (rstr) {
DeepState_GeneratedStrings[DeepState_GeneratedStringsIndex++] = rstr;
for (int i = 0; i < size; i++) {
rstr[i] = DeepState_UCharInRange(1, UCHAR_MAX); /* Disallow null termination till end. */
}
rstr[size] = 0;
} else {
DeepState_LogFormat(DeepState_LogFatal, "Unable to allocate to generate string!");
}
return rstr;
}
/* Function to clean up generated strings, and any other DeepState-managed data. */
extern void DeepState_CleanUp() {
for (int i = 0; i < DeepState_GeneratedStringsIndex; i++) {