Skip to contentSkip to conversation

ui/sdl2: Check return value from g_setenv()

PATCH In review Peter Maydell 2 participants 3 messages Aug 9, 2021 – Aug 10, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/sdl2: Check return value from g_setenv()

Setting environment variables can fail; check the return value from g_setenv() and bail out if we couldn't set SDL_VIDEODRIVER.

Fixes Coverity 1458798

---
I followed existing practice in this function for how it deals with errors (ie, fprintf to stderr and exit).

---
 ui/sdl2.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
ui/sdl2.c +4 -1
index 36d9010cb6c..17c0ec30ebf 100644
@@ -817,7 +817,10 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
      * This is a bit hackish but saves us from bigger problem.
      * Maybe it's a good idea to fix this in SDL instead.
      */
-    g_setenv("SDL_VIDEODRIVER", "x11", 0);
+    if (!g_setenv("SDL_VIDEODRIVER", "x11", 0)) {
+        fprintf(stderr, "Could not set SDL_VIDEODRIVER environment variable\n");
+        exit(1);
+    }
 #endif
 
     if (SDL_Init(SDL_INIT_VIDEO)) {
Signature · 1 line
2.20.1
#1 lore raw

Hi,

Quoted 2 lines

Setting environment variables can fail; check the return value from g_setenv() and bail out if we couldn't set SDL_VIDEODRIVER.

Hmm, looking at the comment I'm wondering whenever we maybe should just drop the setenv (after 6.1). It's quite old, I doubt svgalib talking directly to the hardware is still a thing these days, and we have the x11 -> wayland shift happening ...

take care,
Gerd

#2 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Setting environment variables can fail; check the return value from g_setenv() and bail out if we couldn't set SDL_VIDEODRIVER.

Fixes Coverity 1458798

---
I followed existing practice in this function for how it deals with errors (ie, fprintf to stderr and exit).
---

Queued for 6.1 (given that the "drop setenv altogether" idea clearly is 6.2 material).

thanks,
Gerd