[Mesa-dev] [PATCH] target/d3dadapter9: make sure strings are null-terminated

Eric Engestrom eric at engestrom.ch
Tue May 31 01:21:49 UTC 2016


>From strncpy's man page:
  Warning: If there is no null byte among the first n bytes of src, the
  string placed in dest will not be  null-terminated.

CoverityID: 1320457, 1320458, 1255671

Signed-off-by: Eric Engestrom <eric at engestrom.ch>
---
 src/gallium/targets/d3dadapter9/description.c | 39 ++++++++++++++++++---------
 src/gallium/targets/d3dadapter9/drm.c         |  6 +++--
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/gallium/targets/d3dadapter9/description.c b/src/gallium/targets/d3dadapter9/description.c
index c0a8678..5d4e767 100644
--- a/src/gallium/targets/d3dadapter9/description.c
+++ b/src/gallium/targets/d3dadapter9/description.c
@@ -239,7 +239,8 @@ d3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
     DBG("unknown vendor 0x4%x, emulating 0x4%x\n", drvid->VendorId, fallback_ven);
     drvid->VendorId = fallback_ven;
     drvid->DeviceId = fallback_dev;
-    strncpy(drvid->Description, fallback_name, sizeof(drvid->Description));
+    strncpy(drvid->Description, fallback_name, sizeof(drvid->Description) - 1);
+    drvid->Description[sizeof(drvid->Description) - 1] = 0;
 }
 
 /* fill in driver name and version */
@@ -248,22 +249,26 @@ void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid) {
     case HW_VENDOR_INTEL:
         drvid->DriverVersionLowPart = 0x000A0682;
         drvid->DriverVersionHighPart = 0x0006000F;
-        strncpy(drvid->Driver, "igdumd32.dll", sizeof(drvid->Driver));
+        strncpy(drvid->Driver, "igdumd32.dll", sizeof(drvid->Driver) - 1);
+        drvid->Driver[sizeof(drvid->Driver) - 1] = 0;
         break;
     case HW_VENDOR_VMWARE:
         drvid->DriverVersionLowPart = 0x0001046E;
         drvid->DriverVersionHighPart = 0x0006000E;
-        strncpy(drvid->Driver, "vm3dum.dll", sizeof(drvid->Driver));
+        strncpy(drvid->Driver, "vm3dum.dll", sizeof(drvid->Driver) - 1);
+        drvid->Driver[sizeof(drvid->Driver) - 1] = 0;
         break;
     case HW_VENDOR_AMD:
         drvid->DriverVersionLowPart = 0x000A0500;
         drvid->DriverVersionHighPart = 0x00060011;
-        strncpy(drvid->Driver, "atiumdag.dll", sizeof(drvid->Driver));
+        strncpy(drvid->Driver, "atiumdag.dll", sizeof(drvid->Driver) - 1);
+        drvid->Driver[sizeof(drvid->Driver) - 1] = 0;
         break;
     case HW_VENDOR_NVIDIA:
         drvid->DriverVersionLowPart = 0x000D0FD4;
         drvid->DriverVersionHighPart = 0x00060012;
-        strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
+        strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver) - 1);
+        drvid->Driver[sizeof(drvid->Driver) - 1] = 0;
         break;
     default:
         break;
@@ -277,46 +282,54 @@ void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid) {
     case HW_VENDOR_INTEL:
         for (i = 0; i < sizeof(cards_intel) / sizeof(cards_intel[0]); i++) {
             if (strstr(drvid->Description, cards_intel[i].mesaname)) {
-                strncpy(drvid->Description, cards_intel[i].d3d9name, sizeof(drvid->Description));
+                strncpy(drvid->Description, cards_intel[i].d3d9name, sizeof(drvid->Description) - 1);
+                drvid->Description[sizeof(drvid->Description) - 1] = 0;
                 return;
             }
         }
         /* use a fall-back if nothing matches */
         DBG("Unknown card name %s!\n", drvid->DeviceName);
-        strncpy(drvid->Description, cards_intel[0].d3d9name, sizeof(drvid->Description));
+        strncpy(drvid->Description, cards_intel[0].d3d9name, sizeof(drvid->Description) - 1);
+        drvid->Description[sizeof(drvid->Description) - 1] = 0;
         break;
     case HW_VENDOR_VMWARE:
         for (i = 0; i < sizeof(cards_vmware) / sizeof(cards_vmware[0]); i++) {
             if (strstr(drvid->Description, cards_vmware[i].mesaname)) {
-                strncpy(drvid->Description, cards_vmware[i].d3d9name, sizeof(drvid->Description));
+                strncpy(drvid->Description, cards_vmware[i].d3d9name, sizeof(drvid->Description) - 1);
+                drvid->Description[sizeof(drvid->Description) - 1] = 0;
                 return;
             }
         }
         /* use a fall-back if nothing matches */
         DBG("Unknown card name %s!\n", drvid->DeviceName);
-        strncpy(drvid->Description, cards_vmware[0].d3d9name, sizeof(drvid->Description));
+        strncpy(drvid->Description, cards_vmware[0].d3d9name, sizeof(drvid->Description) - 1);
+        drvid->Description[sizeof(drvid->Description) - 1] = 0;
         break;
     case HW_VENDOR_AMD:
         for (i = 0; i < sizeof(cards_amd) / sizeof(cards_amd[0]); i++) {
             if (strstr(drvid->Description, cards_amd[i].mesaname)) {
-                strncpy(drvid->Description, cards_amd[i].d3d9name, sizeof(drvid->Description));
+                strncpy(drvid->Description, cards_amd[i].d3d9name, sizeof(drvid->Description) - 1);
+                drvid->Description[sizeof(drvid->Description) - 1] = 0;
                 return;
             }
         }
         /* use a fall-back if nothing matches */
         DBG("Unknown card name %s!\n", drvid->DeviceName);
-        strncpy(drvid->Description, cards_amd[0].d3d9name, sizeof(drvid->Description));
+        strncpy(drvid->Description, cards_amd[0].d3d9name, sizeof(drvid->Description) - 1);
+        drvid->Description[sizeof(drvid->Description) - 1] = 0;
         break;
     case HW_VENDOR_NVIDIA:
         for (i = 0; i < sizeof(cards_nvidia) / sizeof(cards_nvidia[0]); i++) {
             if (strstr(drvid->Description, cards_nvidia[i].mesaname)) {
-                strncpy(drvid->Description, cards_nvidia[i].d3d9name, sizeof(drvid->Description));
+                strncpy(drvid->Description, cards_nvidia[i].d3d9name, sizeof(drvid->Description) - 1);
+                drvid->Description[sizeof(drvid->Description) - 1] = 0;
                 return;
             }
         }
         /* use a fall-back if nothing matches */
         DBG("Unknown card name %s!\n", drvid->DeviceName);
-        strncpy(drvid->Description, cards_nvidia[0].d3d9name, sizeof(drvid->Description));
+        strncpy(drvid->Description, cards_nvidia[0].d3d9name, sizeof(drvid->Description) - 1);
+        drvid->Description[sizeof(drvid->Description) - 1] = 0;
         break;
     default:
         break;
diff --git a/src/gallium/targets/d3dadapter9/drm.c b/src/gallium/targets/d3dadapter9/drm.c
index 767a17a..4723786 100644
--- a/src/gallium/targets/d3dadapter9/drm.c
+++ b/src/gallium/targets/d3dadapter9/drm.c
@@ -147,7 +147,8 @@ read_descriptor( struct d3dadapter9_context *ctx,
     snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
                  "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
     strncpy(drvid->Description, ctx->hal->get_name(ctx->hal),
-                 sizeof(drvid->Description));
+                 sizeof(drvid->Description) - 1);
+    drvid->Description[sizeof(drvid->Description) - 1] = 0;
 
     if (override_vendorid > 0) {
         found = FALSE;
@@ -161,7 +162,8 @@ read_descriptor( struct d3dadapter9_context *ctx,
                 drvid->VendorId = fallback_cards[i].vendor_id;
                 drvid->DeviceId = fallback_cards[i].device_id;
                 strncpy(drvid->Description, fallback_cards[i].name,
-                             sizeof(drvid->Description));
+                             sizeof(drvid->Description) - 1);
+                drvid->Description[sizeof(drvid->Description) - 1] = 0;
                 found = TRUE;
                 break;
             }
-- 
2.8.3



More information about the mesa-dev mailing list