Skip to content

Instantly share code, notes, and snippets.

@sjaeckel
Last active September 25, 2019 11:41
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 sjaeckel/103ac2e7711e2e069afc90777ce04ebb to your computer and use it in GitHub Desktop.
Save sjaeckel/103ac2e7711e2e069afc90777ce04ebb to your computer and use it in GitHub Desktop.
Valgrind vararg error
*.c
*.o
*.a
vafail
# this has been tested with gcc-4.8, clang-3.8, clang-7, clang-9 and clang-10
CC=clang-7
CFLAGS=-g
fail: all
${CC} -dumpversion
valgrind ./vafail
all: libvafail.a vafail
.c.o:
${CC} ${CFLAGS} -c $< -o $@
libvafail.a: va_test_fun.o
$(AR) $(ARFLAGS) $@ $<
vafail: vafail.o libvafail.a
${CC} ${CFLAGS} $^ -o $@
clean:
rm *.o *.a vafail
#include <stdarg.h>
#include <stddef.h>
#include <valgrind/memcheck.h>
void va_test_fun(int i, unsigned char *pc, unsigned long *pl, const unsigned char *p, unsigned long l, ...)
{
va_list args;
const unsigned char *curptr;
unsigned long curlen;
va_start(args, l);
curptr = p;
curlen = l;
for (;;) {
VALGRIND_CHECK_VALUE_IS_DEFINED(curptr);
VALGRIND_CHECK_VALUE_IS_DEFINED(curlen);
VALGRIND_CHECK_MEM_IS_DEFINED(curptr, curlen);
/* step to next */
curptr = va_arg(args, const unsigned char*);
if (curptr == NULL) {
break;
}
curlen = va_arg(args, unsigned long);
}
}
#include <stddef.h>
void va_test_fun(int i, unsigned char *pc, unsigned long *pl, const unsigned char *p, unsigned long l, ...);
int main(void)
{
unsigned long l = 123;
unsigned char buf[1024] = {0};
va_test_fun(100, buf, &l, buf, l, buf, 4, NULL, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment