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
Signed-off-by: Rudi Heitbaum <[email protected]>
---
 xf86drm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/xf86drm.c b/xf86drm.c
index d4bfec8a..3199715e 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -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;
 }
 
-- 
2.53.0

Reply via email to