video: Don't rely on memcpy undefined behavior

The C specification states that passing a size of 0 to functions like memcpy is valid, but even if the size is 0 and the function is essentially a no-op, the result when passing any invalid pointers is considered undefined behavior. Don't rely on undefined behavior when copying the display or mode lists.
main
Frank Praznik 2023-02-02 13:43:43 -05:00 committed by Sam Lantinga
parent 166afebcad
commit 47cdb532f1
1 changed files with 18 additions and 13 deletions

View File

@ -634,6 +634,7 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
if (displays) { if (displays) {
int i; int i;
if (_this->displays) {
/* The display list may contain self-referential pointers to the desktop mode. */ /* The display list may contain self-referential pointers to the desktop mode. */
SDL_memcpy(displays, _this->displays, _this->num_displays * sizeof(*displays)); SDL_memcpy(displays, _this->displays, _this->num_displays * sizeof(*displays));
for (i = 0; i < _this->num_displays; ++i) { for (i = 0; i < _this->num_displays; ++i) {
@ -643,8 +644,9 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
} }
SDL_free(_this->displays); SDL_free(_this->displays);
_this->displays = displays; }
_this->displays = displays;
id = _this->next_object_id++; id = _this->next_object_id++;
new_display = &displays[_this->num_displays++]; new_display = &displays[_this->num_displays++];
@ -954,6 +956,7 @@ SDL_bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_Displ
return SDL_FALSE; return SDL_FALSE;
} }
if (display->fullscreen_modes) {
/* Copy the list and update the current mode pointer, if necessary. */ /* Copy the list and update the current mode pointer, if necessary. */
SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes)); SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes));
for (i = 0; i < nmodes; ++i) { for (i = 0; i < nmodes; ++i) {
@ -963,6 +966,8 @@ SDL_bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_Displ
} }
SDL_free(display->fullscreen_modes); SDL_free(display->fullscreen_modes);
}
display->fullscreen_modes = modes; display->fullscreen_modes = modes;
display->max_fullscreen_modes += 32; display->max_fullscreen_modes += 32;
} }