mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-30 21:37:43 +08:00
SDL_iconv_string: simplify recomputation of outbuf and outbytesleft
Noticed this in SDL-1.2 where gcc-13 emits a -Wuse-after-free warning.
No such warning in SDL2 and SDL3, because unlike SDL1.2, SDL_realloc()
is not a macro expanding to libc realloc(). It warns, of course, if
SDL_realloc() is replaced with plain realloc():
src/stdlib/SDL_iconv.c: In function 'SDL_iconv_string_REAL':
src/stdlib/SDL_iconv.c:824:39: warning: pointer 'oldstring' may be used after 'realloc' [-Wuse-after-free]
824 | outbuf = string + (outbuf - oldstring);
| ~~~~~~~~^~~~~~~~~~~~
src/stdlib/SDL_iconv.c:818:30: note: call to 'realloc' here
818 | string = (char *)realloc(string, stringsize + sizeof(Uint32));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This commit is contained in:
@@ -813,6 +813,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
|
|||||||
switch (retCode) {
|
switch (retCode) {
|
||||||
case SDL_ICONV_E2BIG:
|
case SDL_ICONV_E2BIG:
|
||||||
{
|
{
|
||||||
|
const ptrdiff_t diff = (ptrdiff_t) (outbuf - string);
|
||||||
char *oldstring = string;
|
char *oldstring = string;
|
||||||
stringsize *= 2;
|
stringsize *= 2;
|
||||||
string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
|
string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
|
||||||
@@ -821,8 +822,8 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
|
|||||||
SDL_iconv_close(cd);
|
SDL_iconv_close(cd);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
outbuf = string + (outbuf - oldstring);
|
outbuf = string + diff;
|
||||||
outbytesleft = stringsize - (outbuf - string);
|
outbytesleft = stringsize - diff;
|
||||||
SDL_memset(outbuf, 0, sizeof(Uint32));
|
SDL_memset(outbuf, 0, sizeof(Uint32));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user