Permalink
Browse files

segfaulting on purpose is impolite

  • Loading branch information...
zeevt committed Apr 18, 2011
1 parent 20ae891 commit 1895803452f8145d41f1ab103f36fd9e00fd53e2
Showing with 17 additions and 2 deletions.
  1. +1 −1 Makefile
  2. +16 −1 cl_tester.c
View
@@ -22,7 +22,7 @@ cl_test: cl_tester
diff -u testdata/urls.10K afifo && echo "compress-decompress restores original"
rm -f afifo
./cl_tester -S d && echo "decompression is safe"
- ./cl_tester -S c || echo "compression overwrites out buffer"
+ ./cl_tester -S c
check_leaks: cl_tester
valgrind --leak-check=full --show-reachable=yes ./cl_tester -d -c <testdata/urls.10K.snappy >/dev/null
View
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
+#include <signal.h>
#include "csnappy.h"
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
@@ -114,8 +115,17 @@ static int do_compress(FILE *ifile, FILE *ofile)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
+
+static void segfault_handler(int signum) {
+ if (signum == SIGSEGV) {
+ printf("compression overwrites out buffer\n");
+ exit(EXIT_SUCCESS);
+ }
+}
+
int do_selftest_compression(void)
{
+ struct sigaction sa;
char *obuf, *ibuf, *workmem;
FILE *ifile;
long PAGE_SIZE = sysconf(_SC_PAGE_SIZE);
@@ -139,13 +149,18 @@ int do_selftest_compression(void)
handle_error("fclose");
if (!(workmem = (char*)malloc(CSNAPPY_WORKMEM_BYTES)))
handle_error("malloc");
+ sa.sa_handler = segfault_handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sigaction(SIGSEGV, &sa, NULL);
csnappy_compress(ibuf, ilen, obuf, &olen,
workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
+ fprintf(stderr, "ERROR: csnappy_compress did not segfault when should have!\n");
if (munmap(obuf, PAGE_SIZE * 2))
handle_error("munmap");
free(workmem);
free(ibuf);
- return 0;
+ return EXIT_FAILURE;
}
static const char fake[] = "\x32\xc4\x66\x6f\x6f\x6f\x6f\x6f\x6f";

0 comments on commit 1895803

Please sign in to comment.