Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1581441pxb; Mon, 8 Mar 2021 00:46:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyXxpLzMyUEMcVmGZLkHq4U8NJEY+1wb7BtboBPaJMQKCPQZT2ZRp1c2jnRRPvMfwAv8sA+ X-Received: by 2002:a05:6402:704:: with SMTP id w4mr21738366edx.175.1615193182393; Mon, 08 Mar 2021 00:46:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615193182; cv=none; d=google.com; s=arc-20160816; b=H5mf7ddRgB01xQfmL2OxpIdh85ozxuQRe9ZK5QUDpAYl58dtOgUv2KpmdrWFVK/McI Ej+Cji4GlromuGhJjlWtZLG7vschCxmzzce4NtzsvdX5JHOb7LaJCHxR1O85aSQamH8G s/M1PSnpc50aqh/b5WklsnOiFB+LCCSHnzLWDXuZRAL6oRlGjB6d9YIlE81ErmeaVPez DuXfIvrgVJC3xOuOw//6K2uKm2zK93THUE9IksDxgV7YZ0l8UQL3N5VRzmngMDRRppkk LQCMhtKE91p6i8NSaX4T768S2qOCEzDzt1+XAAnrGoSAJUB5O3nUoqLurHWZ783KhsXy /TIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=0km9WDqZ/wm3fOomYUmzhJ6b9+i6T1lcGguSYo91hak=; b=uhcsEHeLC13Eg8pY/Nr/dLWR8NkAKKrfS/jnCHh91hDCyfPaa7Hg35Q4KpNiB/MlUc R3x4kFbK7qPLDfPfhc/RpdkNsQ8scbC5u9lhrC/Z0IQccuEMBCNMS17ePpPcVV8E3UKY zXpNdyNk5r+CLT9hHSyjVP5hQLryqBb+hAbMuMdfiwChVJmNp3VtOYaxGXSxjvWjY81a 5KY0VJISzyt286vI/Fzu6fQfCPNavbEHJDbx2QTN9SbI90dO6eU8zw3hgs2nyHELEsyq luiV+12U61VUuywKgKMDDflU/R1fBnzUy9ClWPA+omcsAqaYJ4iDG8LLx8+1vYCBAEQf 8vWQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v25si6626058ejw.423.2021.03.08.00.46.00; Mon, 08 Mar 2021 00:46:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229965AbhCHIcY (ORCPT + 99 others); Mon, 8 Mar 2021 03:32:24 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:13138 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230457AbhCHIcS (ORCPT ); Mon, 8 Mar 2021 03:32:18 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DvBNz6wqGz16HjZ; Mon, 8 Mar 2021 16:30:27 +0800 (CST) Received: from huawei.com (10.69.192.56) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Mon, 8 Mar 2021 16:32:05 +0800 From: Luo Jiaxing To: , , CC: , , , Subject: [PATCH v1] drm/nouveau/device: use snprintf() to replace strncpy() to avoid NUL-terminated string loss Date: Mon, 8 Mar 2021 16:32:52 +0800 Message-ID: <1615192372-47348-1-git-send-email-luojiaxing@huawei.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.69.192.56] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Following warning is found when using W=1 to build kernel: In function ‘nvkm_udevice_info’, inlined from ‘nvkm_udevice_mthd’ at drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:195:10: drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:164:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation] 164 | strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:165:2: warning: ‘strncpy’ specified bound 64 equals destination size [-Wstringop-truncation] 165 | strncpy(args->v0.name, device->name, sizeof(args->v0.name)); The reason of this warning is strncpy() does not guarantee that the destination buffer will be NUL terminated. If the length of source string is bigger than number we set by third input parameter, only a part of characters is copied to the destination, and no NUL-terminated string is automatically added. There are some potential risks. So use snprintf() to replace strncpy(). Signed-off-by: Luo Jiaxing --- drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c index fea9d8f..4bf65bb 100644 --- a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +++ b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c @@ -161,8 +161,8 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size) if (imem && args->v0.ram_size > 0) args->v0.ram_user = args->v0.ram_user - imem->reserved; - strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); - strncpy(args->v0.name, device->name, sizeof(args->v0.name)); + snprintf(args->v0.chip, sizeof(args->v0.chip), "%s", device->chip->name); + snprintf(args->v0.name, sizeof(args->v0.name), "%s", device->name); return 0; } -- 2.7.4