From 872608b8af25d769bc4cc2a1f30cb2bea17d3f6a Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 21 Jun 2024 21:01:35 +0200
Subject: [PATCH] Don't allocate in SDLTest_GenerateRunSeed
---
include/SDL3/SDL_test_harness.h | 12 +++++------
src/test/SDL_test_harness.c | 38 ++++++++++-----------------------
test/testautomation_sdltest.c | 16 ++++++++------
3 files changed, 27 insertions(+), 39 deletions(-)
diff --git a/include/SDL3/SDL_test_harness.h b/include/SDL3/SDL_test_harness.h
index 54dc2dc829fdd..c8ad19bc2d31a 100644
--- a/include/SDL3/SDL_test_harness.h
+++ b/include/SDL3/SDL_test_harness.h
@@ -101,15 +101,15 @@ typedef struct SDLTest_TestSuiteReference {
/*
- * Generates a random run seed string for the harness. The generated seed will contain alphanumeric characters (0-9A-Z).
+ * Generates a random run seed string for the harness. The generated seed
+ * will contain alphanumeric characters (0-9A-Z).
*
- * Note: The returned string needs to be deallocated by the caller.
+ * \param buffer Buffer in which to generate the random seed. Must have a capacity of at least length + 1 characters.
+ * \param length Number of alphanumeric characters to write to buffer, must be >0
*
- * \param length The length of the seed string to generate
- *
- * \returns the generated seed string
+ * \returns A null-terminated seed string and equal to the in put buffer on success, NULL on failure
*/
-char *SDLTest_GenerateRunSeed(const int length);
+char *SDLTest_GenerateRunSeed(char *buffer, int length);
/*
* Execute a test suite using the given run seed and execution key.
diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c
index f1fc68f921636..6f3161d28c062 100644
--- a/src/test/SDL_test_harness.c
+++ b/src/test/SDL_test_harness.c
@@ -50,32 +50,19 @@
/* ! Timeout for single test case execution */
static Uint32 SDLTest_TestCaseTimeout = 3600;
-/**
- * Generates a random run seed string for the harness. The generated seed
- * will contain alphanumeric characters (0-9A-Z).
- *
- * Note: The returned string needs to be deallocated by the caller.
- *
- * \param length The length of the seed string to generate
- *
- * \returns The generated seed string
- */
-char *SDLTest_GenerateRunSeed(const int length)
+char *SDLTest_GenerateRunSeed(char *buffer, int length)
{
- char *seed = NULL;
Uint64 randomContext = SDL_GetPerformanceCounter();
int counter;
- /* Sanity check input */
- if (length <= 0) {
- SDLTest_LogError("The length of the harness seed must be >0.");
+ if (!buffer) {
+ SDLTest_LogError("Input buffer must not be NULL.");
return NULL;
}
- /* Allocate output buffer */
- seed = (char *)SDL_malloc((length + 1) * sizeof(char));
- if (!seed) {
- SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
+ /* Sanity check input */
+ if (length <= 0) {
+ SDLTest_LogError("The length of the harness seed must be >0.");
return NULL;
}
@@ -88,11 +75,11 @@ char *SDLTest_GenerateRunSeed(const int length)
} else {
ch = (char)('A' + v - 10);
}
- seed[counter] = ch;
+ buffer[counter] = ch;
}
- seed[length] = '\0';
+ buffer[length] = '\0';
- return seed;
+ return buffer;
}
/**
@@ -417,14 +404,11 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user
/* Generate run see if we don't have one already */
if (!userRunSeed || userRunSeed[0] == '\0') {
- char *tmp = SDLTest_GenerateRunSeed(16);
- if (!tmp) {
+ runSeed = SDLTest_GenerateRunSeed(generatedSeed, 16);
+ if (!runSeed) {
SDLTest_LogError("Generating a random seed failed");
return 2;
}
- SDL_memcpy(generatedSeed, tmp, 16 + 1);
- SDL_free(tmp);
- runSeed = generatedSeed;
} else {
runSeed = userRunSeed;
}
diff --git a/test/testautomation_sdltest.c b/test/testautomation_sdltest.c
index fa7c7eb840c9d..4b6d9eed1ff05 100644
--- a/test/testautomation_sdltest.c
+++ b/test/testautomation_sdltest.c
@@ -16,26 +16,30 @@
*/
static int sdltest_generateRunSeed(void *arg)
{
+ char buffer[32];
char *result;
size_t i, l;
int j;
for (i = 1; i <= 10; i += 3) {
- result = SDLTest_GenerateRunSeed((int)i);
- SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed()");
+ result = SDLTest_GenerateRunSeed(buffer, (int)i);
+ SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(<buf>, %" SDL_PRIu64 ")", (Uint64)i);
SDLTest_AssertCheck(result != NULL, "Verify returned value is not NULL");
if (result != NULL) {
l = SDL_strlen(result);
SDLTest_AssertCheck(l == i, "Verify length of returned value is %d, got: %d", (int)i, (int)l);
- SDL_free(result);
}
}
+ result = SDLTest_GenerateRunSeed(NULL, 10);
+ SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(NULL, 10)");
+ SDLTest_AssertCheck(result == NULL, "Verify returned value is NULL");
+
/* Negative cases */
for (j = -2; j <= 0; j++) {
- result = SDLTest_GenerateRunSeed(j);
- SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed()");
- SDLTest_AssertCheck(result == NULL, "Verify returned value is not NULL");
+ result = SDLTest_GenerateRunSeed(buffer, j);
+ SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(<buf>, %d)", j);
+ SDLTest_AssertCheck(result == NULL, "Verify returned value is NULL");
}
return TEST_COMPLETED;