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
parent
166afebcad
commit
47cdb532f1
|
@ -634,17 +634,19 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
|
||||||
if (displays) {
|
if (displays) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* The display list may contain self-referential pointers to the desktop mode. */
|
if (_this->displays) {
|
||||||
SDL_memcpy(displays, _this->displays, _this->num_displays * sizeof(*displays));
|
/* The display list may contain self-referential pointers to the desktop mode. */
|
||||||
for (i = 0; i < _this->num_displays; ++i) {
|
SDL_memcpy(displays, _this->displays, _this->num_displays * sizeof(*displays));
|
||||||
if (displays[i].current_mode == &_this->displays[i].desktop_mode) {
|
for (i = 0; i < _this->num_displays; ++i) {
|
||||||
displays[i].current_mode = &displays[i].desktop_mode;
|
if (displays[i].current_mode == &_this->displays[i].desktop_mode) {
|
||||||
|
displays[i].current_mode = &displays[i].desktop_mode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,15 +956,18 @@ SDL_bool SDL_AddFullscreenDisplayMode(SDL_VideoDisplay *display, const SDL_Displ
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy the list and update the current mode pointer, if necessary. */
|
if (display->fullscreen_modes) {
|
||||||
SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes));
|
/* Copy the list and update the current mode pointer, if necessary. */
|
||||||
for (i = 0; i < nmodes; ++i) {
|
SDL_memcpy(modes, display->fullscreen_modes, nmodes * sizeof(*modes));
|
||||||
if (display->current_mode == &display->fullscreen_modes[i]) {
|
for (i = 0; i < nmodes; ++i) {
|
||||||
display->current_mode = &modes[i];
|
if (display->current_mode == &display->fullscreen_modes[i]) {
|
||||||
|
display->current_mode = &modes[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue