[Mesa-dev] [PATCH] d3dadapter9: use snprintf(..., "%s", ...) instead of strncpy

Axel Davy davyaxel0 at gmail.com
Sun Nov 25 16:23:02 UTC 2018


Reading 
https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/
I think the snprintf variant suffers from the same issue, and the 
compiler is just not yet able to detect it,
and send the same warning (but it might do in later gcc versions).

Probably a better fix would be to copy with a max size of 
sizeof(drvid->Description)-1 and do
drvid->Description[sizeof(drvid->Description)-1] = '\0';
(Though the webpage says only doing the assignment should be sufficient 
to please gcc).

Axel

On 25/11/2018 10:49, Andre Heider wrote:
> Fixes -Wstringop-truncation compiler warnings.
> See f836d799f9066adf58f36 "intel/decoder: use snprintf(..., "%s", ...) instead of strncpy"
>
> Signed-off-by: Andre Heider <a.heider at gmail.com>
> ---
>   src/gallium/targets/d3dadapter9/description.c | 27 ++++++++++++-------
>   src/gallium/targets/d3dadapter9/drm.c         |  8 +++---
>   2 files changed, 22 insertions(+), 13 deletions(-)
>
> diff --git a/src/gallium/targets/d3dadapter9/description.c b/src/gallium/targets/d3dadapter9/description.c
> index c0a86782f8..a3e4cd6177 100644
> --- a/src/gallium/targets/d3dadapter9/description.c
> +++ b/src/gallium/targets/d3dadapter9/description.c
> @@ -20,6 +20,7 @@
>    * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
>    * USE OR OTHER DEALINGS IN THE SOFTWARE. */
>   
> +#include <stdio.h>
>   #include <string.h>
>   #include "adapter9.h"
>   
> @@ -239,7 +240,7 @@ 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));
> +    snprintf(drvid->Description, sizeof(drvid->Description), "%s", fallback_name);
>   }
>   
>   /* fill in driver name and version */
> @@ -277,46 +278,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));
> +                snprintf(drvid->Description, sizeof(drvid->Description),
> +                         "%s", cards_intel[i].d3d9name);
>                   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));
> +        snprintf(drvid->Description, sizeof(drvid->Description),
> +                 "%s", cards_intel[0].d3d9name);
>           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));
> +                snprintf(drvid->Description, sizeof(drvid->Description),
> +                         "%s", cards_vmware[i].d3d9name);
>                   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));
> +        snprintf(drvid->Description, sizeof(drvid->Description),
> +                 "%s", cards_vmware[0].d3d9name);
>           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));
> +                snprintf(drvid->Description, sizeof(drvid->Description),
> +                         "%s", cards_amd[i].d3d9name);
>                   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));
> +        snprintf(drvid->Description, sizeof(drvid->Description),
> +                 "%s", cards_amd[0].d3d9name);
>           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));
> +                snprintf(drvid->Description, sizeof(drvid->Description),
> +                         "%s", cards_nvidia[i].d3d9name);
>                   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));
> +        snprintf(drvid->Description, sizeof(drvid->Description),
> +                 "%s", cards_nvidia[0].d3d9name);
>           break;
>       default:
>           break;
> diff --git a/src/gallium/targets/d3dadapter9/drm.c b/src/gallium/targets/d3dadapter9/drm.c
> index 6fb8caf5c2..e08778b81d 100644
> --- a/src/gallium/targets/d3dadapter9/drm.c
> +++ b/src/gallium/targets/d3dadapter9/drm.c
> @@ -149,8 +149,8 @@ read_descriptor( struct d3dadapter9_context *ctx,
>                    &drvid->SubSysId, &drvid->Revision);
>       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));
> +    snprintf(drvid->Description, sizeof(drvid->Description),
> +                 "%s", ctx->hal->get_name(ctx->hal));
>   
>       if (override_vendorid > 0) {
>           found = FALSE;
> @@ -163,8 +163,8 @@ read_descriptor( struct d3dadapter9_context *ctx,
>                           fallback_cards[i].device_id);
>                   drvid->VendorId = fallback_cards[i].vendor_id;
>                   drvid->DeviceId = fallback_cards[i].device_id;
> -                strncpy(drvid->Description, fallback_cards[i].name,
> -                             sizeof(drvid->Description));
> +                snprintf(drvid->Description, sizeof(drvid->Description),
> +                             "%s", fallback_cards[i].name);
>                   found = TRUE;
>                   break;
>               }




More information about the mesa-dev mailing list