From 2b2693ae9008df5d212e268d3241cef7cb5bfe48 Mon Sep 17 00:00:00 2001
From: Pierre Wendling <[EMAIL REDACTED]>
Date: Sun, 18 Sep 2022 08:49:56 -0400
Subject: [PATCH] N3DS: Use asprintf instead of snprintf.
---
src/file/n3ds/SDL_rwopsromfs.c | 9 +++++++--
src/filesystem/n3ds/SDL_sysfilesystem.c | 13 +++++++++----
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/src/file/n3ds/SDL_rwopsromfs.c b/src/file/n3ds/SDL_rwopsromfs.c
index fe92c3d3be76..c75323f61ffd 100644
--- a/src/file/n3ds/SDL_rwopsromfs.c
+++ b/src/file/n3ds/SDL_rwopsromfs.c
@@ -20,6 +20,7 @@
*/
#include "SDL_rwopsromfs.h"
+#include "SDL_error.h"
/* Nintendo 3DS applications may embed resources in the executable. The
resources are stored in a special read-only partition prefixed with
@@ -30,7 +31,7 @@ FILE *
N3DS_FileOpen(const char *file, const char *mode)
{
FILE *fp = NULL;
- char romfs_path[4096];
+ char *romfs_path;
/* romfs are read-only */
if (SDL_strchr(mode, 'r') == NULL) {
@@ -43,13 +44,17 @@ N3DS_FileOpen(const char *file, const char *mode)
return fopen(file, mode);
}
- SDL_snprintf(romfs_path, 4096, "romfs:/%s", file);
+ if (SDL_asprintf(&romfs_path, "romfs:/%s", file) < 0) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
fp = fopen(romfs_path, mode);
if (fp == NULL) {
fp = fopen(file, mode);
}
+ SDL_free(romfs_path);
return fp;
}
diff --git a/src/filesystem/n3ds/SDL_sysfilesystem.c b/src/filesystem/n3ds/SDL_sysfilesystem.c
index e402e037ca0a..06b4cd65b79b 100644
--- a/src/filesystem/n3ds/SDL_sysfilesystem.c
+++ b/src/filesystem/n3ds/SDL_sysfilesystem.c
@@ -52,6 +52,10 @@ SDL_GetPrefPath(const char *org, const char *app)
}
pref_path = MakePrefPath(app);
+ if (pref_path == NULL) {
+ return NULL;
+ }
+
if (CreatePrefPathDir(pref_path) < 0) {
SDL_free(pref_path);
return NULL;
@@ -63,10 +67,11 @@ SDL_GetPrefPath(const char *org, const char *app)
SDL_FORCE_INLINE char *
MakePrefPath(const char *app)
{
- static const char *FMT = "/3ds/%s/";
- size_t length = SDL_snprintf(NULL, 0, FMT, app) + 1;
- char *pref_path = (char *) SDL_calloc(length, sizeof(char));
- SDL_snprintf(pref_path, length, FMT, app);
+ char *pref_path;
+ if (SDL_asprintf(&pref_path, "/3ds/%s/", app) < 0) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
return pref_path;
}