Skip to content

Commit

Permalink
Merge pull request #138 from Tarsnap/libcperciva-import
Browse files Browse the repository at this point in the history
Libcperciva import
  • Loading branch information
cperciva committed Jun 28, 2018
2 parents 1f02b68 + ff7d232 commit ff7f046
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
7 changes: 7 additions & 0 deletions libcperciva/util/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ size_t
getopt_lookup(const char * os)
{

/*
* We only take this parameter so that we can assert that it's the
* same option string as we returned from getopt(); as such, it is
* unused in the absense of assertions.
*/
(void)os; /* UNUSED */

/* Can't reset here. */
if (optreset)
DIE("Can't reset in the middle of getopt loop");
Expand Down
44 changes: 29 additions & 15 deletions tests/shared_test_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,35 @@ ensure_valgrind_suppression() {
valgrind_suppressions="${out_valgrind}/suppressions"
valgrind_suppressions_log="${out_valgrind}/suppressions.pre"

# Run valgrind on the binary, sending it a "\n" so that
# a test which uses STDIN will not wait for user input.
printf "\n" | (valgrind --leak-check=full --show-leak-kinds=all \
--gen-suppressions=all \
--log-file=${valgrind_suppressions_log} \
${potential_memleaks_binary})

# Strip out useless parts from the log file, as well as
# removing references to the main and "pl_*" ("potential
# loss") functions so that the suppressions can apply to
# other binaries.
(grep -v "^==" ${valgrind_suppressions_log} \
| grep -v " fun:pl_" - \
| grep -v " fun:main" - \
> ${valgrind_suppressions} ) || true
# Start off with an empty suppression file
touch ${valgrind_suppressions}

# Get list of tests
${potential_memleaks_binary} | while read testname; do
this_valgrind_supp="${valgrind_suppressions_log}-${testname}"

# Run valgrind on the binary, sending it a "\n" so that
# a test which uses STDIN will not wait for user input.
printf "\n" | (valgrind \
--leak-check=full --show-leak-kinds=all \
--gen-suppressions=all \
--suppressions=${valgrind_suppressions} \
--log-file=${this_valgrind_supp} \
${potential_memleaks_binary} \
${testname})

# Append name to suppressions file
printf "# ${testname}\n" >> ${valgrind_suppressions}

# Strip out useless parts from the log file, as well as
# removing references to the main and "pl_*" ("potential loss")
# functions so that the suppressions can apply to other
# binaries. Append to suppressions file.
(grep -v "^==" ${this_valgrind_supp} \
| grep -v " fun:pl_" - \
| grep -v " fun:main" - \
>> ${valgrind_suppressions} ) || true
done

# Clean up
rm -f ${valgrind_suppressions_log}
Expand Down
26 changes: 23 additions & 3 deletions tests/valgrind/potential-memleaks.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FGETS_BUFSIZE 64

Expand All @@ -13,12 +14,31 @@ pl_freebsd_fgets()
exit(1);
}

#define MEMLEAKTEST(x) { #x, x }
static const struct memleaktest {
const char * name;
void (* func)(void);
} tests[] = {
MEMLEAKTEST(pl_freebsd_fgets)
};
static const int num_tests = sizeof(tests) / sizeof(tests[0]);

int
main()
main(int argc, char * argv[])
{
int i;

/* Test potential memory leaks. */
pl_freebsd_fgets();
if (argc == 2) {
/* Run the relevant function. */
for (i = 0; i < num_tests; i++) {
if ((strcmp(argv[1], tests[i].name)) == 0)
tests[i].func();
}
} else {
/* Print test names. */
for (i = 0; i < num_tests; i++)
printf("%s\n", tests[i].name);
}

/* Success! */
exit(0);
Expand Down

0 comments on commit ff7f046

Please sign in to comment.