Skip to content

Instantly share code, notes, and snippets.

@josephg
Last active March 5, 2017 09:58
Show Gist options
  • Save josephg/35158c17546d53f3795e to your computer and use it in GitHub Desktop.
Save josephg/35158c17546d53f3795e to your computer and use it in GitHub Desktop.
AFL harness for librope
#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