[Mesa-dev] [PATCH v2] target/d3dadapter9: deduplicate string handling code
Eric Engestrom
eric.engestrom at imgtec.com
Wed Aug 24 13:06:57 UTC 2016
Also make sure strings are null-terminated.
>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
CC: Emil Velikov <emil.l.velikov at gmail.com>
Signed-off-by: Eric Engestrom <eric.engestrom at imgtec.com>
---
I know a couple people said this string termination bug is unlikely to ever hit,
but it doesn't hurt to make sure, especially since it costs virtually nothing.
---
src/gallium/targets/d3dadapter9/description.c | 80 +++++++++++++--------------
1 file changed, 37 insertions(+), 43 deletions(-)
diff --git a/src/gallium/targets/d3dadapter9/description.c b/src/gallium/targets/d3dadapter9/description.c
index c0a8678..6f29ff6 100644
--- a/src/gallium/targets/d3dadapter9/description.c
+++ b/src/gallium/targets/d3dadapter9/description.c
@@ -239,86 +239,80 @@ 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 */
void d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid) {
+ const char* driver_name;
+
switch (drvid->VendorId) {
case HW_VENDOR_INTEL:
drvid->DriverVersionLowPart = 0x000A0682;
drvid->DriverVersionHighPart = 0x0006000F;
- strncpy(drvid->Driver, "igdumd32.dll", sizeof(drvid->Driver));
+ driver_name = "igdumd32.dll";
break;
case HW_VENDOR_VMWARE:
drvid->DriverVersionLowPart = 0x0001046E;
drvid->DriverVersionHighPart = 0x0006000E;
- strncpy(drvid->Driver, "vm3dum.dll", sizeof(drvid->Driver));
+ driver_name = "vm3dum.dll";
break;
case HW_VENDOR_AMD:
drvid->DriverVersionLowPart = 0x000A0500;
drvid->DriverVersionHighPart = 0x00060011;
- strncpy(drvid->Driver, "atiumdag.dll", sizeof(drvid->Driver));
+ driver_name = "atiumdag.dll";
break;
case HW_VENDOR_NVIDIA:
drvid->DriverVersionLowPart = 0x000D0FD4;
drvid->DriverVersionHighPart = 0x00060012;
- strncpy(drvid->Driver, "nvd3dum.dll", sizeof(drvid->Driver));
+ driver_name = "nvd3dum.dll";
break;
default:
- break;
+ return;
}
+
+ strncpy(drvid->Driver, driver_name, sizeof(drvid->Driver) - 1);
+ drvid->Driver[sizeof(drvid->Driver) - 1] = 0;
}
/* try to match the device name and override it with Windows like device names */
void d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid) {
+ struct card_lookup_table *cards;
+ unsigned cards_num;
unsigned i;
+
switch (drvid->VendorId) {
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));
- 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));
+ cards = &cards_intel[0];
+ cards_num = sizeof(cards_intel) / sizeof(cards_intel[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));
- 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));
+ cards = &cards_vmware[0];
+ cards_num = sizeof(cards_vmware) / sizeof(cards_vmware[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));
- 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));
+ cards = &cards_amd[0];
+ cards_num = sizeof(cards_amd) / sizeof(cards_amd[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));
- 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));
+ cards = &cards_nvidia[0];
+ cards_num = sizeof(cards_nvidia) / sizeof(cards_nvidia[0]);
break;
default:
- break;
+ return;
}
+
+ for (i = 0; i < cards_num; i++) {
+ if (strstr(drvid->Description, cards[i].mesaname)) {
+ strncpy(drvid->Description, cards[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[0].d3d9name, sizeof(drvid->Description) - 1);
+ drvid->Description[sizeof(drvid->Description) - 1] = 0;
}
--
2.9.3
More information about the mesa-dev
mailing list