Skip to content

Instantly share code, notes, and snippets.

@dndx

dndx/test.c Secret

Created April 3, 2017 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dndx/45cdea90139cd031e8b604d7aad80614 to your computer and use it in GitHub Desktop.
Save dndx/45cdea90139cd031e8b604d7aad80614 to your computer and use it in GitHub Desktop.
Program to reproduce the PCRE JIT Valgrind warning
#include <stdio.h>
#include <string.h>
#include "pcre.h"
int main(void) {
pcre_extra *sd;
pcre *re;
const char *errstr;
int erroff, rc;
const char *ver = pcre_version();
char *reg = strdup("b");
char *sub = strdup("a");
fprintf(stderr, "running: %s\n", ver);
re = pcre_compile(reg, 0, &errstr, &erroff, NULL);
if (!re) {
fprintf(stderr, "pcre_compile() failed");
return 1;
}
sd = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &errstr);
if (!sd || errstr) {
fprintf(stderr, "pcre_study() failed");
return 1;
}
rc = pcre_exec(re, sd, sub, 1, 0, 0, NULL,
0);
if (rc != PCRE_ERROR_NOMATCH) {
fprintf(stderr, "pcre_exec() failed: %d\n", rc);
}
pcre_free_study(sd);
pcre_free(re);
free(reg);
free(sub);
return 0;
}
@agentzh
Copy link

agentzh commented Apr 3, 2017

The result is:

$ ./configure --enable-jit --prefix=$HOME/pcre-8.4-build --enable-valgrind
$ make -j4 && make install
$ cd $HOME/pcre-8.4-build/lib
$ gcc -g test.c libpcre.a
$ valgrind ./a.out 
==11428== Memcheck, a memory error detector
==11428== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11428== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==11428== Command: ./a.out
==11428== 
running: 8.40 2017-01-11
==11428== Invalid read of size 16
==11428==    at 0x4C1307B: ???
==11428==    by 0x4C2E08F: ???
==11428==    by 0xFFEFF7D2F: ???
==11428==  Address 0x4c2e090 is 0 bytes inside a block of size 2 alloc'd
==11428==    at 0x4A06C50: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==11428==    by 0x3E61E8B079: strdup (in /usr/lib64/libc-2.21.so)
==11428==    by 0x400C2E: main (test.c:12)
==11428== 
==11428== 
==11428== HEAP SUMMARY:
==11428==     in use at exit: 0 bytes in 0 blocks
==11428==   total heap usage: 10 allocs, 10 frees, 8,634 bytes allocated
==11428== 
==11428== All heap blocks were freed -- no leaks are possible
==11428== 
==11428== For counts of detected and suppressed errors, rerun with: -v
==11428== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment