Skip to content

Commit

Permalink
Drop usage of alloca() in tests
Browse files Browse the repository at this point in the history
Reported-by: @rsbeckerca (github.com)
  • Loading branch information
rockdaboot authored and eli-schwartz committed Feb 11, 2024
1 parent 4a75ae3 commit 2c151c0
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 49 deletions.
7 changes: 7 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ PSL_TESTS = test-is-public test-is-public-all test-is-cookie-domain-acceptable

if ENABLE_BUILTIN
PSL_TESTS += test-is-public-builtin test-registrable-domain
test_is_public_builtin_SOURCES = test-is-public-builtin.c $(common_SOURCES)
test_registrable_domain_SOURCES = test-registrable-domain.c $(common_SOURCES)
endif

check_PROGRAMS = $(PSL_TESTS)

TESTS_ENVIRONMENT = TESTS_VALGRIND="@VALGRIND_ENVIRONMENT@"
TESTS = $(PSL_TESTS)

common_SOURCES = common.c common.h
test_is_public_SOURCES = test-is-public.c $(common_SOURCES)
test_is_public_all_SOURCES = test-is-public-all.c $(common_SOURCES)
test_is_cookie_domain_acceptable_SOURCES = test-is-cookie-domain-acceptable.c $(common_SOURCES)

# dafsa.psl and dafsa_ascii.psl must be created before any test is executed
# check-local target works in parallel to the tests, so the test suite will likely fail
BUILT_SOURCES = psl.dafsa psl_ascii.dafsa
Expand Down
48 changes: 48 additions & 0 deletions tests/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright(c) 2014-2024 Tim Ruehsen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* This file is part of the test suite of libpsl.
*/

#include <stdio.h> // snprintf
#include <stdlib.h> // exit, system
#include <string.h> // strlen
#if defined _WIN32
# include <malloc.h>
#endif

int run_valgrind(const char *valgrind, const char *executable)
{
char cmd[BUFSIZ];
int n, rc;

n = snprintf(cmd, sizeof(cmd), "TESTS_VALGRIND="" %s %s", valgrind, executable);
if ((unsigned)n >= sizeof(cmd)) {
printf("Valgrind command line is too long (>= %u)\n", (unsigned) sizeof(cmd));
return EXIT_FAILURE;
}

if ((rc = system(cmd))) {
printf("Failed to execute with '%s' (system() returned %d)\n", valgrind, rc);
}

return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
38 changes: 38 additions & 0 deletions tests/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright(c) 2014-2024 Tim Ruehsen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* This file is part of the test suite of libpsl.
*/

#ifndef COMMON_H
#define COMMON_H

#ifdef __cplusplus
extern "C" {
#endif

int run_valgrind(const char *valgrind, const char *executable);

#ifdef __cplusplus
}
#endif

#endif /* COMMON_H */
4 changes: 2 additions & 2 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ if enable_builtin
endif

foreach test_name : tests
source = test_name + '.c'
exe = executable(test_name, source,
sources = [test_name + '.c', 'common.c', 'common.h']
exe = executable(test_name, sources,
build_by_default: false,
c_args : tests_cargs,
link_with : libpsl,
Expand Down
10 changes: 2 additions & 8 deletions tests/test-is-cookie-domain-acceptable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif

#include <libpsl.h>
#include "common.h"

#define countof(a) (sizeof(a)/sizeof(*(a)))

Expand Down Expand Up @@ -130,11 +128,7 @@ int main(int argc, const char * const *argv)
const char *valgrind = getenv("TESTS_VALGRIND");

if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
char *cmd = alloca(cmdsize);

snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0;
return run_valgrind(valgrind, argv[0]);
}
}

Expand Down
14 changes: 5 additions & 9 deletions tests/test-is-public-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined _WIN32
#if defined _WIN32
# include <malloc.h>
#endif

#include <libpsl.h>
#include "common.h"

static int
ok,
Expand Down Expand Up @@ -120,7 +119,7 @@ static void test_psl_entry(const psl_ctx_t *psl, const char *domain, int type)
} else if (*domain == '*') { /* a wildcard, e.g. *.ck or *.platform.sh */
/* '*.platform.sh' -> 'y.x.platform.sh' */
size_t len = strlen(domain);
char *xdomain = alloca(len + 3);
char *xdomain = malloc(len + 3);

memcpy(xdomain, "y.x", 3);
memcpy(xdomain + 3, domain + 1, len);
Expand All @@ -129,6 +128,7 @@ static void test_psl_entry(const psl_ctx_t *psl, const char *domain, int type)
test_type_any(psl, xdomain + 2, type, 1); /* random wildcard-matching domain is a PS... */
test_type_any(psl, xdomain, type, 0); /* ... but sub domain is not */

free(xdomain);
} else {
test_type_any(psl, domain, type, 1); /* Any normal PSL entry */
}
Expand Down Expand Up @@ -230,11 +230,7 @@ int main(int argc, const char * const *argv)
const char *valgrind = getenv("TESTS_VALGRIND");

if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
char *cmd = alloca(cmdsize);

snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0;
return run_valgrind(valgrind, argv[0]);
}
}

Expand Down
12 changes: 2 additions & 10 deletions tests/test-is-public-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined _WIN32
# include <malloc.h>
#endif

#include <libpsl.h>
#include "common.h"

#define countof(a) (sizeof(a)/sizeof(*(a)))

Expand Down Expand Up @@ -141,11 +137,7 @@ int main(int argc, const char * const *argv)
const char *valgrind = getenv("TESTS_VALGRIND");

if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
char *cmd = alloca(cmdsize);

snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0;
return run_valgrind(valgrind, argv[0]);
}
}

Expand Down
12 changes: 2 additions & 10 deletions tests/test-is-public.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined _WIN32
# include <malloc.h>
#endif

#include <libpsl.h>
#include "common.h"

#define countof(a) (sizeof(a)/sizeof(*(a)))

Expand Down Expand Up @@ -195,11 +191,7 @@ int main(int argc, const char * const *argv)
const char *valgrind = getenv("TESTS_VALGRIND");

if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
char *cmd = alloca(cmdsize);

snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0;
return run_valgrind(valgrind, argv[0]);
}
}

Expand Down
12 changes: 2 additions & 10 deletions tests/test-registrable-domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined _WIN32
# include <malloc.h>
#endif

#include <libpsl.h>
#include "common.h"

static int
ok,
Expand Down Expand Up @@ -199,11 +195,7 @@ int main(int argc, const char * const *argv)
const char *valgrind = getenv("TESTS_VALGRIND");

if (valgrind && *valgrind) {
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
char *cmd = alloca(cmdsize);

snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
return system(cmd) != 0;
return run_valgrind(valgrind, argv[0]);
}
}

Expand Down

0 comments on commit 2c151c0

Please sign in to comment.