Skip to contentSkip to conversation

drm: handle asprintf() allocation failures in format modifier name functions

PATCH New Rudi Heitbaum 1 participant 1 message Apr 2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: handle asprintf() allocation failures in format modifier name functions

Check the return value of asprintf() in drmGetFormatModifierNameFromNvidia(), drmGetFormatModifierNameFromAmlogic(), and drmGetFormatModifierNameFromVivante() to avoid leaving pointers uninitialized on allocation failure.

asprintf() returns -1 on failure and does not guarantee the output pointer is set to NULL, which could lead to undefined behavior if the pointer is used subsequently. Set the pointer to NULL explicitly on failure.

Fixes -Wunused-result warnings from GCC
---
 xf86drm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xf86drm.c +7 -4
index d4bfec8a..3199715e 100644
@@ -348,9 +348,10 @@ drmGetFormatModifierNameFromNvidia(uint64_t modifier)
      * testing against TEGRA_TILE */
     if ((modifier & 0x10) == 0x10) {
         char *mod_nvidia;
-        asprintf(&mod_nvidia, "BLOCK_LINEAR_2D,HEIGHT=%"PRIu64",KIND=%"PRIu64","
+        if(asprintf(&mod_nvidia, "BLOCK_LINEAR_2D,HEIGHT=%"PRIu64",KIND=%"PRIu64","
                  "GEN=%"PRIu64",SECTOR=%"PRIu64",COMPRESSION=%"PRIu64"", height,
-                 kind, gen, sector, compression);
+                 kind, gen, sector, compression) < 0)
+            mod_nvidia = NULL;
         return mod_nvidia;
     }
 
@@ -542,7 +543,8 @@ drmGetFormatModifierNameFromAmlogic(uint64_t modifier)
     else
         opts_str = "0";
 
-    asprintf(&mod_amlogic, "FBC,LAYOUT=%s,OPTIONS=%s", layout_str, opts_str);
+    if(asprintf(&mod_amlogic, "FBC,LAYOUT=%s,OPTIONS=%s", layout_str, opts_str) < 0)
+        mod_amlogic = NULL;
     return mod_amlogic;
 }
 
@@ -606,7 +608,8 @@ drmGetFormatModifierNameFromVivante(uint64_t modifier)
 	break;
     }
 
-    asprintf(&mod_vivante, "%s%s%s", color_tiling, tile_status, compression);
+    if(asprintf(&mod_vivante, "%s%s%s", color_tiling, tile_status, compression) < 0)
+        mod_vivante = NULL;
     return mod_vivante;
 }
 
Signature · 1 line
2.53.0