Last active
March 5, 2017 09:58
-
-
Save josephg/35158c17546d53f3795e to your computer and use it in GitHub Desktop.
AFL harness for librope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include "rope.h" | |
int main() { | |
printf("AFL test harness\n"); | |
rope *r = rope_new(); | |
char *buffer = NULL; | |
size_t buf_cap = 0; | |
while (true) { | |
// First read the position we're editing the rope | |
ssize_t bytes_read = getline(&buffer, &buf_cap, stdin); | |
if (bytes_read == -1) break; | |
int pos = atoi(buffer); | |
int length = (int)rope_char_count(r); | |
pos = pos < 0 ? 0 : pos > length ? length : pos; | |
// Now read the characters to insert | |
bytes_read = getline(&buffer, &buf_cap, stdin); | |
if (bytes_read == -1) break; | |
if (bytes_read > 0 && buffer[0] == '-') { | |
// Delete some characters | |
int to_del = atoi(&buffer[1]); | |
rope_del(r, pos, to_del); | |
} else { | |
// Delete the newline. | |
if (bytes_read > 0) buffer[bytes_read - 1] = '\0'; | |
ROPE_RESULT result = rope_insert(r, pos, (uint8_t *)buffer); | |
if (result == ROPE_INVALID_UTF8) { | |
fprintf(stderr, "invalid utf8 - insert ignored\n"); | |
} | |
} | |
} | |
_rope_check(r); | |
printf("Final length: %zu\n", rope_char_count(r)); | |
rope_free(r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment