Fix every memory defect the checker found, and bump to 0.4.0
Six findings, all of them libakgl's, all closed. The memcheck run is clean. Not one of the four json_load_file calls in src/ was ever matched by a json_decref, so every asset load abandoned its parsed document: 1.5 KB per sprite, 2.1 KB per character, 9 KB per load of the 2x2 fixture map, and on the order of a megabyte for a real one. Each loader now releases it in the CLEANUP block it already had, on the success path as well as the failure one. akgl_registry_load_properties needed its loop moved inside the ATTEMPT block first: props is a borrowed reference into the document and is read after the block ends, so every exit from that loop leaked the whole tree. akgl_get_property copied a fixed AKGL_MAX_STRING_LENGTH bytes out of what SDL handed back, which is SDL's strdup of the value -- four bytes for "0.0". That read up to 4 KiB past the end of somebody else's allocation on every property read, returned whatever was there past the terminator, and would have faulted on a value that landed at the end of a page. It copies the value and its terminator now, and refuses a value too long for an akgl_String rather than truncating it into an unterminated buffer. The header note that described the overread as a quirk describes correct behaviour instead. The four savegame name tables wrote a fixed-width field starting at the registry key, and SDL sizes that allocation to the name. They read past it on every entry and put what they found into the save file: up to half a kilobyte of this process's heap per registered object, in a file a player might send to somebody. They stage through a zeroed buffer now, and a negative-array-size typedef fails the build if a table's width ever outgrows it. akgl_controller_list_keyboards never freed the array SDL_GetKeyboards allocated for it. A font could be opened and published and never handed back -- there was no way to close one, so a game that changed fonts between scenes leaked ten kilobytes each time, and loading over a live name leaked the font it displaced. akgl_text_unloadfont is that missing half, and akgl_text_loadfont calls it when it replaces a name, after the new font has opened so a failed reload leaves the caller with the font they had. A new public symbol takes the version to 0.4.0 and the soname with it: an 0.3 consumer cannot be handed this library and told it is the same ABI. tests/registry.c fills a destination with a sentinel and asserts the bytes past the terminator survive a read, which fails against the old copy. tests/text.c covers unload, double unload, unloading a name that was never registered, and replacement closing the displaced font. The JSON releases have no test of their own and cannot sensibly have one -- nothing in the public API can observe a jansson refcount -- so the memcheck run is their test, which is an argument for gating it rather than against. The two remaining findings are in deps/semver's own unit test, which is vendored. They are suppressed by function name, so a rewrite of those cases comes back as a finding. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -218,7 +218,7 @@ static akerr_ErrorContext *akgl_character_load_json_inner(json_t *json, akgl_Cha
|
||||
akerr_ErrorContext *akgl_character_load_json(char *filename)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
json_t *json;
|
||||
json_t *json = NULL;
|
||||
json_error_t error;
|
||||
akgl_Character *obj = NULL;
|
||||
//akgl_String *tmpstr = NULL;
|
||||
@@ -245,6 +245,13 @@ akerr_ErrorContext *akgl_character_load_json(char *filename)
|
||||
CATCH(errctx, akgl_get_json_number_value(json, "acceleration_y", &obj->ay));
|
||||
} CLEANUP {
|
||||
//IGNORE(akgl_heap_release_string(tmpstr));
|
||||
// The character keeps nothing that points into the document -- every
|
||||
// field above is copied out of it -- so the whole parsed tree goes back
|
||||
// here, on the success path as well as the failure one.
|
||||
if ( json != NULL ) {
|
||||
json_decref(json);
|
||||
json = NULL;
|
||||
}
|
||||
if ( errctx != NULL ) {
|
||||
if ( errctx->status != 0 ) {
|
||||
IGNORE(akgl_heap_release_character(obj));
|
||||
|
||||
@@ -190,10 +190,19 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_controller_list_keyboards(void)
|
||||
|
||||
FAIL_ZERO_RETURN(e, keyboards, AKERR_NULLPOINTER, "%s", SDL_GetError());
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
const char *name = SDL_GetKeyboardNameForID(keyboards[i]);
|
||||
SDL_Log("Keyboard %d: ID %u, Name: %s\n", i, keyboards[i], name);
|
||||
}
|
||||
// The array is SDL's to allocate and ours to free -- that is the contract on
|
||||
// every SDL_Get*s() enumeration. Released in CLEANUP so the loop can fail
|
||||
// without taking the array with it.
|
||||
ATTEMPT {
|
||||
for (int i = 0; i < count; i++) {
|
||||
const char *name = SDL_GetKeyboardNameForID(keyboards[i]);
|
||||
SDL_Log("Keyboard %d: ID %u, Name: %s\n", i, keyboards[i], name);
|
||||
}
|
||||
} CLEANUP {
|
||||
SDL_free(keyboards);
|
||||
keyboards = NULL;
|
||||
} PROCESS(e) {
|
||||
} FINISH(e, true);
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
|
||||
74
src/game.c
74
src/game.c
@@ -50,6 +50,72 @@ static akerr_ErrorContext *write_exact(const void *ptr, size_t size, size_t nmem
|
||||
return aksl_fwrite(ptr, size, nmemb, fp, &transferred);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Widest name field any of the save tables writes.
|
||||
*
|
||||
* The spritesheet table is the widest at #AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH;
|
||||
* the other three are name lengths half that or less.
|
||||
*/
|
||||
#define AKGL_GAME_SAVE_MAX_NAME_FIELD AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH
|
||||
|
||||
/**
|
||||
* @brief Fail the build if a table ever declares a field wider than the staging buffer.
|
||||
*
|
||||
* A negative array size is a compile error, so widening one of these constants
|
||||
* without widening #AKGL_GAME_SAVE_MAX_NAME_FIELD cannot get past the compiler
|
||||
* and turn back into the overread this buffer exists to prevent.
|
||||
*/
|
||||
typedef char akgl_game_save_name_field_fits[
|
||||
((AKGL_ACTOR_MAX_NAME_LENGTH <= AKGL_GAME_SAVE_MAX_NAME_FIELD) &&
|
||||
(AKGL_SPRITE_MAX_NAME_LENGTH <= AKGL_GAME_SAVE_MAX_NAME_FIELD) &&
|
||||
(AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH <= AKGL_GAME_SAVE_MAX_NAME_FIELD) &&
|
||||
(AKGL_SPRITE_MAX_CHARACTER_NAME_LENGTH <= AKGL_GAME_SAVE_MAX_NAME_FIELD)) ? 1 : -1];
|
||||
|
||||
/**
|
||||
* @brief Write a registry key as a fixed-width, zero-padded field.
|
||||
*
|
||||
* The name tables are read back without a length prefix, so every entry has to
|
||||
* occupy exactly @p width bytes. Writing @p width bytes *from the key itself*
|
||||
* is what the four iterators used to do, and the key is an SDL allocation sized
|
||||
* to the name -- 40 bytes for `benchactor0`. That read past the end of it on
|
||||
* every entry, and put whatever was next to it in the heap into the save file:
|
||||
* up to half a kilobyte of this process's memory per registered object, in a
|
||||
* file a player might send to somebody else.
|
||||
*
|
||||
* Staging through a zeroed buffer costs one `memset` per entry and makes the
|
||||
* padding deterministic, which the reader wanted anyway.
|
||||
*
|
||||
* @param name The registry key. Required, and NUL-terminated -- it comes from
|
||||
* SDL's property table, which stores C strings.
|
||||
* @param width Field width in bytes, including the padding. Required to be at
|
||||
* most #AKGL_GAME_SAVE_MAX_NAME_FIELD; the compile-time check
|
||||
* above is what keeps that true for the four call sites.
|
||||
* @param fp Open output stream. Required.
|
||||
* @return `NULL` on success, otherwise an error context owned by the caller.
|
||||
* @throws AKERR_NULLPOINTER If @p name or @p fp is `NULL`.
|
||||
* @throws AKERR_OUTOFBOUNDS If @p width exceeds the staging buffer.
|
||||
*/
|
||||
static akerr_ErrorContext *write_name_field(const char *name, size_t width, FILE *fp)
|
||||
{
|
||||
char field[AKGL_GAME_SAVE_MAX_NAME_FIELD];
|
||||
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, name, AKERR_NULLPOINTER, "NULL name");
|
||||
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL file pointer");
|
||||
FAIL_NONZERO_RETURN(
|
||||
e,
|
||||
(width > sizeof(field)),
|
||||
AKERR_OUTOFBOUNDS,
|
||||
"Name field width %zu exceeds the staging buffer (%zu)",
|
||||
width,
|
||||
sizeof(field));
|
||||
|
||||
memset(&field, 0x00, sizeof(field));
|
||||
strncpy((char *)&field, name, (width - 1));
|
||||
PASS(e, write_exact((char *)&field, 1, width, fp));
|
||||
SUCCEED_RETURN(e);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *read_exact(void *ptr, size_t size, size_t nmemb, FILE *fp)
|
||||
{
|
||||
size_t transferred;
|
||||
@@ -216,7 +282,7 @@ void akgl_game_save_actorname_iterator(void *userdata, SDL_PropertiesID props, c
|
||||
PREPARE_ERROR(e);
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(e, fp, AKERR_NULLPOINTER, "NULL file pointer");
|
||||
CATCH(e, write_exact((char *)name, 1, AKGL_ACTOR_MAX_NAME_LENGTH, fp));
|
||||
CATCH(e, write_name_field(name, AKGL_ACTOR_MAX_NAME_LENGTH, fp));
|
||||
actor = SDL_GetPointerProperty(props, name, NULL);
|
||||
CATCH(e, write_exact(&actor, 1, sizeof(akgl_Actor *), fp));
|
||||
} CLEANUP {
|
||||
@@ -245,7 +311,7 @@ void akgl_game_save_spritename_iterator(void *userdata, SDL_PropertiesID props,
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(e, fp, AKERR_NULLPOINTER, "NULL file pointer");
|
||||
sprite = SDL_GetPointerProperty(props, name, NULL);
|
||||
CATCH(e, write_exact((char *)name, 1, AKGL_SPRITE_MAX_NAME_LENGTH, fp));
|
||||
CATCH(e, write_name_field(name, AKGL_SPRITE_MAX_NAME_LENGTH, fp));
|
||||
CATCH(e, write_exact(&sprite, 1, sizeof(akgl_Sprite *), fp));
|
||||
} CLEANUP {
|
||||
} PROCESS(e) {
|
||||
@@ -277,7 +343,7 @@ void akgl_game_save_spritesheetname_iterator(void *userdata, SDL_PropertiesID pr
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(e, fp, AKERR_NULLPOINTER, "NULL file pointer");
|
||||
spritesheet = SDL_GetPointerProperty(props, name, NULL);
|
||||
CATCH(e, write_exact((char *)name, 1, AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH, fp));
|
||||
CATCH(e, write_name_field(name, AKGL_SPRITE_SHEET_MAX_FILENAME_LENGTH, fp));
|
||||
CATCH(e, write_exact(&spritesheet, 1, sizeof(akgl_SpriteSheet *), fp));
|
||||
} CLEANUP {
|
||||
} PROCESS(e) {
|
||||
@@ -305,7 +371,7 @@ void akgl_game_save_charactername_iterator(void *userdata, SDL_PropertiesID prop
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(e, fp, AKERR_NULLPOINTER, "NULL file pointer");
|
||||
character = SDL_GetPointerProperty(props, name, NULL);
|
||||
CATCH(e, write_exact((char *)name, 1, AKGL_SPRITE_MAX_CHARACTER_NAME_LENGTH, fp));
|
||||
CATCH(e, write_name_field(name, AKGL_SPRITE_MAX_CHARACTER_NAME_LENGTH, fp));
|
||||
CATCH(e, write_exact(&character, 1, sizeof(akgl_Character *), fp));
|
||||
} CLEANUP {
|
||||
} PROCESS(e) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string.h>
|
||||
#include <akerror.h>
|
||||
#include <jansson.h>
|
||||
|
||||
@@ -129,6 +130,13 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_registry_load_properties(char *fname)
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, fname, AKERR_NULLPOINTER, "null filename");
|
||||
// One ATTEMPT around the whole function, rather than one for the parse and
|
||||
// a bare loop after it. `props` is a borrowed reference into `json` and is
|
||||
// read by the loop, so the document cannot be released until the loop is
|
||||
// done -- and with the loop outside the block, every path out of it leaked
|
||||
// the document. The per-property ATTEMPT stays nested inside the loop,
|
||||
// which is what AGENTS.md requires: a CATCH written directly in a loop
|
||||
// breaks the loop rather than leaving the function.
|
||||
ATTEMPT {
|
||||
SDL_Log("Loading from %s", fname);
|
||||
json = json_load_file(fname, 0, &error);
|
||||
@@ -141,21 +149,28 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_registry_load_properties(char *fname)
|
||||
error.line,
|
||||
error.text);
|
||||
CATCH(errctx, akgl_get_json_object_value(json, "properties", &props));
|
||||
|
||||
json_object_foreach(props, pkey, pvalue) {
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_heap_next_string(&tmpstr));
|
||||
CATCH(errctx, akgl_get_json_string_value(props, (char *)pkey, &tmpstr));
|
||||
SDL_SetStringProperty(AKGL_REGISTRY_PROPERTIES, pkey, tmpstr->data);
|
||||
SDL_Log("Set property %s = %s", pkey, tmpstr->data);
|
||||
CATCH(errctx, akgl_heap_release_string(tmpstr));
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
}
|
||||
} CLEANUP {
|
||||
// Every value was copied into the property store on the way past, so
|
||||
// nothing outlives the document.
|
||||
if ( json != NULL ) {
|
||||
json_decref(json);
|
||||
json = NULL;
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
|
||||
json_object_foreach(props, pkey, pvalue) {
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_heap_next_string(&tmpstr));
|
||||
CATCH(errctx, akgl_get_json_string_value(props, (char *)pkey, &tmpstr));
|
||||
SDL_SetStringProperty(AKGL_REGISTRY_PROPERTIES, pkey, tmpstr->data);
|
||||
SDL_Log("Set property %s = %s", pkey, tmpstr->data);
|
||||
CATCH(errctx, akgl_heap_release_string(tmpstr));
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
}
|
||||
SDL_Log("Properties loaded");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -171,6 +186,9 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_set_property(char *name, char *src)
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgl_get_property(char *name, akgl_String **dest, char *def)
|
||||
{
|
||||
const char *value = NULL;
|
||||
size_t valuelen = 0;
|
||||
|
||||
PREPARE_ERROR(e);
|
||||
FAIL_ZERO_RETURN(e, name, AKERR_NULLPOINTER, "NULL char *");
|
||||
FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "NULL akgl_String *");
|
||||
@@ -178,12 +196,32 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_get_property(char *name, akgl_String **d
|
||||
if ( *dest == NULL ) {
|
||||
CATCH(e, akgl_heap_next_string(dest));
|
||||
}
|
||||
CATCH(e,
|
||||
aksl_memcpy(
|
||||
(*dest)->data,
|
||||
(void *)SDL_GetStringProperty(AKGL_REGISTRY_PROPERTIES, (const char *)name, (const char *)def),
|
||||
AKGL_MAX_STRING_LENGTH)
|
||||
);
|
||||
// Copy the value's own length, not the destination's capacity. What
|
||||
// SDL hands back is its strdup of the value -- four bytes for "0.0" --
|
||||
// so copying a fixed AKGL_MAX_STRING_LENGTH read up to 4 KiB past the
|
||||
// end of somebody else's allocation on every call. It returned garbage
|
||||
// past the terminator, and it would have faulted outright on a value
|
||||
// that happened to sit at the end of a page.
|
||||
value = SDL_GetStringProperty(AKGL_REGISTRY_PROPERTIES, (const char *)name, (const char *)def);
|
||||
// An unset property with a NULL default still reports AKERR_NULLPOINTER,
|
||||
// which is what the header has always promised. It used to arrive from
|
||||
// inside aksl_memcpy; now it is raised here, before anything is measured.
|
||||
FAIL_ZERO_BREAK(
|
||||
e,
|
||||
value,
|
||||
AKERR_NULLPOINTER,
|
||||
"Property %s is not set and no default was given",
|
||||
name);
|
||||
valuelen = strlen(value);
|
||||
FAIL_NONZERO_BREAK(
|
||||
e,
|
||||
(valuelen >= AKGL_MAX_STRING_LENGTH),
|
||||
AKERR_OUTOFBOUNDS,
|
||||
"Property %s is %zu bytes, which does not fit an akgl_String (%d)",
|
||||
name,
|
||||
valuelen,
|
||||
AKGL_MAX_STRING_LENGTH);
|
||||
CATCH(e, aksl_memcpy((*dest)->data, (void *)value, (valuelen + 1)));
|
||||
} CLEANUP {
|
||||
} PROCESS(e) {
|
||||
} FINISH(e, true);
|
||||
|
||||
@@ -167,6 +167,13 @@ akerr_ErrorContext *akgl_sprite_load_json(char *filename)
|
||||
CATCH(errctx, akgl_get_json_array_index_integer((json_t *)frames, i, (uint32_t *)&obj->frameids[i]));
|
||||
}
|
||||
} CLEANUP {
|
||||
// The sprite copies every field it wants out of the document and the
|
||||
// sheet is found by resolved path, so nothing here points into the
|
||||
// parsed tree once this block runs. Released on both paths.
|
||||
if ( json != NULL ) {
|
||||
json_decref(json);
|
||||
json = NULL;
|
||||
}
|
||||
if ( errctx != NULL && errctx->status != 0 ) {
|
||||
IGNORE(akgl_heap_release_sprite(obj));
|
||||
IGNORE(akgl_heap_release_spritesheet(sheet));
|
||||
|
||||
24
src/text.c
24
src/text.c
@@ -19,6 +19,13 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_loadfont(char *name, char *filepath
|
||||
FAIL_ZERO_RETURN(errctx, filepath, AKERR_NULLPOINTER, "Null filepath");
|
||||
font = TTF_OpenFont(filepath, size);
|
||||
FAIL_ZERO_RETURN(errctx, font, AKGL_ERR_SDL, "%s", SDL_GetError());
|
||||
// Loading over an existing name used to abandon the font it displaced --
|
||||
// there was nothing in the library that could close one. The old font goes
|
||||
// back first now, and only once the new one has opened, so a failed load
|
||||
// leaves the caller with the font they already had.
|
||||
if ( SDL_GetPointerProperty(AKGL_REGISTRY_FONT, name, NULL) != NULL ) {
|
||||
PASS(errctx, akgl_text_unloadfont(name));
|
||||
}
|
||||
FAIL_ZERO_RETURN(
|
||||
errctx,
|
||||
SDL_SetPointerProperty(AKGL_REGISTRY_FONT, name, (void *)font),
|
||||
@@ -30,6 +37,23 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_loadfont(char *name, char *filepath
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgl_text_unloadfont(char *name)
|
||||
{
|
||||
TTF_Font *font = NULL;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, name, AKERR_NULLPOINTER, "Null font name");
|
||||
|
||||
font = (TTF_Font *)SDL_GetPointerProperty(AKGL_REGISTRY_FONT, name, NULL);
|
||||
FAIL_ZERO_RETURN(errctx, font, AKERR_KEY, "No font named %s in the registry", name);
|
||||
|
||||
// Cleared before the close, so a font is never reachable through the
|
||||
// registry after it has been handed back to SDL_ttf.
|
||||
SDL_ClearProperty(AKGL_REGISTRY_FONT, name);
|
||||
TTF_CloseFont(font);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgl_text_rendertextat(TTF_Font *font, char *text, SDL_Color color, int wraplength, int x, int y)
|
||||
{
|
||||
SDL_Surface *textsurf = NULL;
|
||||
|
||||
@@ -729,6 +729,14 @@ akerr_ErrorContext *akgl_tilemap_load(char *fname, akgl_Tilemap *dest)
|
||||
}
|
||||
} CLEANUP {
|
||||
//IGNORE(akgl_heap_release_string(tmpstr));
|
||||
// The map is built entirely out of copies -- layer data, tileset
|
||||
// geometry, object names -- so the document is dead the moment the
|
||||
// loaders above return, whether they succeeded or not. It is also the
|
||||
// largest of the four: a map's JSON is the size of its layer data.
|
||||
if ( json != NULL ) {
|
||||
json_decref(json);
|
||||
json = NULL;
|
||||
}
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
|
||||
Reference in New Issue
Block a user