From 9e642f9ed4edb2eec708e5f9b74186754e94d1a5 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Fri, 25 Apr 2025 03:31:25 +0200 Subject: [PATCH] fixed valgrind-found bugs in generic test code, also fixed its cpp build --- array_handle_gen_test.c | 4 ++-- bugsy.c | 48 +++++++++++++++++++++++++++++++++++++++++ makefile | 15 +++++++++++++ raii_test.c | 2 +- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 bugsy.c diff --git a/array_handle_gen_test.c b/array_handle_gen_test.c index 8f2e6b9..197bd36 100644 --- a/array_handle_gen_test.c +++ b/array_handle_gen_test.c @@ -7,9 +7,9 @@ Array(uint64_t, Array_uint64); int main() { - int n; + size_t n = 0; printf("Number of elements to sum: "); - scanf(" %d", &n); + scanf(" %zu", &n); /* Create a 64 bit array of 'n' elements */ creat(Array_uint64, data, &n); // printf("vektor.count:%d\n", data.count); diff --git a/bugsy.c b/bugsy.c new file mode 100644 index 0000000..c3ec413 --- /dev/null +++ b/bugsy.c @@ -0,0 +1,48 @@ +#include +#include +#include + +enum HANDLE_STATE { + HANDLE_CREAT, + HANDLE_DESTR +}; + + +struct Array_uint64 { size_t count; uint64_t *v; }; +static inline void Array_uint64_lifetime_handler( + enum HANDLE_STATE state, + struct Array_uint64 *self, + void *data) { + if(state == HANDLE_CREAT) { + self->count = *(size_t*) data; + printf("self: %zd, count: %d", self, self->count); + self->v = (uint64_t*) calloc(self->count, sizeof(uint64_t)); + } else if (state == HANDLE_DESTR) { + if(self->v) free(self->v); + } +}; + +int main() { + // XXX: size_t n = 0; + int n; // FIXME: BUG + printf("Number of elements to sum: "); + // XXX: scanf(" %zu", &n); + scanf(" %d", &n); // FIXME: BUG + + + [[gnu::always_inline]] inline void data_cleanuper(struct Array_uint64 *v) { + Array_uint64_lifetime_handler(HANDLE_DESTR, v, ((void *)0) ); + } + + [[gnu::cleanup(data_cleanuper)]] struct Array_uint64 data; + Array_uint64_lifetime_handler(HANDLE_CREAT, & data, &n);; + + for(int i = 0; i < data.count; ++i) { + scanf(" %d", &n); + data.v[i] = n; + } + n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i]; + printf("Sum: %d\n", n); + + return 0; +} diff --git a/makefile b/makefile index 7046ba5..2261b31 100644 --- a/makefile +++ b/makefile @@ -14,6 +14,21 @@ gcc: g++ array_test.c -o array_test_cpp gcc array_handle_gen_test.c -o array_handle_gen_test g++ array_handle_gen_test.c -o array_handle_gen_test_cpp +debug: + gcc -g macroname.c -o macroname + gcc -g count.c -o count + gcc -g innerf.c -o innerf + # g++ innerf.c -o innerf_cpp # does not work :-( + gcc -g construct_destruct.c -o construct_destruct + gcc -g raii_test.c -o raii_test + g++ -g raii_test.cpp -o raii_test_cpp + gcc -g handle_test.c -o handle_test + gcc -g array_handle_test.c -o array_handle_test + g++ -g array_handle_test.c -o array_handle_test_cpp + gcc -g array_test.c -o array_test + g++ -g array_test.c -o array_test_cpp + gcc -g array_handle_gen_test.c -o array_handle_gen_test + g++ -g array_handle_gen_test.c -o array_handle_gen_test_cpp clang: clang macroname.c -o macroname clang count.c -o count diff --git a/raii_test.c b/raii_test.c index 7dd646d..e57797c 100644 --- a/raii_test.c +++ b/raii_test.c @@ -17,7 +17,7 @@ int main() { cs += bytes[i]; } - printf("Checksum of found bytes: %d\n", cs); + printf("Checksum of found bytes: %d\n", (int) cs); // exit(0); // This does not release resources! return 0;