Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp329594pxb; Thu, 25 Feb 2021 03:51:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPkMisdrBXSskJYhKSD4c74P3WIOIf7X1hsWQ2xIM7nwm6DFUHtwplN65DSOn3YjKJclxu X-Received: by 2002:aa7:de0f:: with SMTP id h15mr2561873edv.140.1614253869488; Thu, 25 Feb 2021 03:51:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614253869; cv=none; d=google.com; s=arc-20160816; b=qJH3muNaZzM3xKGE+XgTapoDMb+/4r+9tF/ByAXkOxEFATP9uaIlbsz8FcSY+xuS10 aj1nJzXAZoVaXh0+6BKwCib/95BLb8Z5KzZPSyBOUtfS0GmKSUoG4jdE+2pC01NW4wQ4 +TO8sc6cly5XGFGB4b1J7yu9H/f+kiO2QLgvhekI3tk2+nUJvc9TBzPCSaALu8C/MRbU BNT1ptHBAU6kSyHAmxa2PmBUQjlbzutjwqKGFYN/7UuUJPr7lS375gHChH1L5pqhsYpX zqASWwQRKO5ZbuiUI9cTcfAT6ifboSdElYgwKVY0qPeeqRXS2STJPFcQq/4xhaNaFCQB 8Ujg== 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=ja1P7JVCrsT4H4tHtPMgTuxeqvpc/DxL+e+5LN5zlp0=; b=DykFKsAxGCQMaA0CyXi807DciVoACFMpAP5xUUGEgvifVSwQ5NNPqV07IQjqJQVDQu HvOFpXz2LedRoOL9fzm1ISVr6arzO5j1ISKFBHlfnK5Q97ixsqtnLWp8o9v8b7RRIiJ8 QFmDhaA3JooO4VK6/xAmDQFVJ9GuNG7FoY7luWZgmTtW53Y8uMIPj5wXfih7uu6jMBix 1j8Rh5S150ugSL00cjME4jdFhyAU0snbL9Pr/1i/0fv8JeGSeBmvzo33EVk39OqVwqTI AnUYScVYrjEAX/S0IyncuM87/pfyS7b/xfx0OPqIQZxDiuHfjlR5urKBjJNbSKfLh0Iz VJYQ== 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 h28si346376edj.553.2021.02.25.03.50.46; Thu, 25 Feb 2021 03:51:09 -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 S229993AbhBYLjF (ORCPT + 99 others); Thu, 25 Feb 2021 06:39:05 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:13088 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231895AbhBYLjE (ORCPT ); Thu, 25 Feb 2021 06:39:04 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DmW2l0pD6z16CZf; Thu, 25 Feb 2021 19:36:31 +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; Thu, 25 Feb 2021 19:37:58 +0800 From: Luo Jiaxing To: , , CC: , , Subject: [PATCH v1] drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy() Date: Thu, 25 Feb 2021 19:38:52 +0800 Message-ID: <1614253132-21793-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 first [number] of characters is copied to the destination, and no NUL-terminated is automatically added. There are some potential risks. Signed-off-by: Luo Jiaxing --- drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 6 ++++-- 1 file changed, 4 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..2a32fe0 100644 --- a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +++ b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c @@ -161,8 +161,10 @@ 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)); + strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip) - 1); + args->v0.chip[sizeof(args->v0.chip) - 1] = '\0'; + strncpy(args->v0.name, device->name, sizeof(args->v0.name) - 1); + args->v0.name[sizeof(args->v0.name) - 1] = '\0'; return 0; } -- 2.7.4