From patchwork Tue Mar 13 13:21:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 131468 Delivered-To: patch@linaro.org Received: by 10.46.84.17 with SMTP id i17csp731463ljb; Tue, 13 Mar 2018 06:22:48 -0700 (PDT) X-Google-Smtp-Source: AG47ELtdCbO+ZeZmhEIdh/CuPMZ6ht+SWDuCIx/jRSHXph7Kuq021reZpQQrAdx7Al74mOO81TA+ X-Received: by 10.99.62.193 with SMTP id l184mr480305pga.87.1520947368031; Tue, 13 Mar 2018 06:22:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520947368; cv=none; d=google.com; s=arc-20160816; b=o3yV+p6aAKwfyqqlLPcCKk47cnflHm3O0BcDNrrHvHRYJXvSm1xGMPlNETLBCuruGl za6WRHGa1vUF9zqAXGjE0idBPywdzbbA0iTzcls8VNMxrgnNWVoQf5nHJ82Emq9rpPKo rddO19B4EtvV9TctR+JOB7jNvCieItdQftJEqF/5CsDz00FGeGKFH9+0Az9JKzaWX4Jf RdM3kQKBLPC0FBtjpXjw3c7zdgE/sU39aB93M4lmZSQaDHRuNtawjCiNJvgasJ4emzxs B6nKiabSW8eNaHytC8kzQc9/BSfkv8U7pFOW6r6W1i5nZEZGgaNNO3DgMvhTvu3e7h/a K4NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=IThGEErWxbuiznpy2GEW+ZXMBySoZBOwUzscrk1ZPK0=; b=awj3+0c0I3qEyCqgb90Be1GsErqmpZfv/duDvU8XDxVP9cqTRSWUHGRtITJZ0JClQJ jjewmVsBWktnFLX+fuSqOuKIQ/ItnwvVdaK9KDx+nHo8hXosRbt3v6Cw/XN88RcFffLV tYuqRDVhe3J+a0k2Cui4aIljqj1BfdXQ9K0dicQOCJSO6JPvSH6oUakgDfL5QT9pBQKn x7DypqhjurNLF9vqvNEzXpMse8J+HXMqL1OSATpw4fve4+cyRIzUVUXNFBE5bFAZ+wRt 0yWUXaKMGpX777oL4xCxbsT3hlE+Vua5hneayV9ahw7x3He3tQpuGHrbxJ/gK2phu/e0 C5wg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v77si152068pfa.108.2018.03.13.06.22.47; Tue, 13 Mar 2018 06:22:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752738AbeCMNWo (ORCPT + 28 others); Tue, 13 Mar 2018 09:22:44 -0400 Received: from mout.kundenserver.de ([212.227.17.13]:60299 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752084AbeCMNW0 (ORCPT ); Tue, 13 Mar 2018 09:22:26 -0400 Received: from wuerfel.lan ([95.208.111.237]) by mrelayeu.kundenserver.de (mreue105 [212.227.15.145]) with ESMTPA (Nemesis) id 0Lp6ay-1ePguU0MkO-00eudw; Tue, 13 Mar 2018 14:22:02 +0100 From: Arnd Bergmann To: "David S. Miller" , Herbert Xu , Paul Blakey Cc: Arnd Bergmann , Martin Sebor , Florian Westphal , Phil Sutter , Tom Herbert , linux-kernel@vger.kernel.org Subject: [PATCH] test_rhashtable: avoid gcc-8 -Wformat-overflow warning Date: Tue, 13 Mar 2018 14:21:39 +0100 Message-Id: <20180313132155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RSWQySMIU 7YmGaXZa7lRLSoQR5vED01DcsZj0GjyVdK9OBg== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org gcc-8 warns about a code pattern that is used in the newly added test_rhashtable code: lib/test_rhashtable.c: In function 'print_ht': lib/test_rhashtable.c:511:21: error: ' bucket[' directive writing 8 bytes into a region of size between 1 and 512 [-Werror=format-overflow=] sprintf(buff, "%s\nbucket[%d] -> ", buff, i); ^~~~~~~~~ lib/test_rhashtable.c:511:4: note: 'sprintf' output between 15 and 536 bytes into a destination of size 512 sprintf(buff, "%s\nbucket[%d] -> ", buff, i); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The problem here is using the same fixed-length buffer as input and output of snprintf(), which for an unbounded loop has an actual potential to overflow the buffer. The '512' byte length was apparently chosen to be "long enough" to prevent that in practice, but without any specific guarantees of being the smallest safe size. I can see three possible ways to avoid this warning: - rewrite the code to use pointer arithmetic to forward the buffer, rather than copying the buffer itself. This is a more conventional use of sprintf(), and it avoids the warning, but is not any more safe than the original code. - Rewrite the function in a safe way that avoids both the potential overflow and the warning. - Ask the gcc developers to not warn for this pattern if we consider the warning to be inappropriate. This patch implements the first of the above, as an illustration of the problem, and the simplest workaround. Fixes: 499ac3b60f65 ("test_rhashtable: add test case for rhltable with duplicate objects") Cc: Martin Sebor Signed-off-by: Arnd Bergmann --- My patch is untested, please try it out before applying. --- lib/test_rhashtable.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) -- 2.9.0 diff --git a/lib/test_rhashtable.c b/lib/test_rhashtable.c index f4000c137dbe..a0f4fb03d2de 100644 --- a/lib/test_rhashtable.c +++ b/lib/test_rhashtable.c @@ -496,6 +496,7 @@ static unsigned int __init print_ht(struct rhltable *rhlt) struct rhashtable *ht; const struct bucket_table *tbl; char buff[512] = ""; + char *buffp = buff; unsigned int i, cnt = 0; ht = &rhlt->ht; @@ -508,18 +509,18 @@ static unsigned int __init print_ht(struct rhltable *rhlt) next = !rht_is_a_nulls(pos) ? rht_dereference(pos->next, ht) : NULL; if (!rht_is_a_nulls(pos)) { - sprintf(buff, "%s\nbucket[%d] -> ", buff, i); + buffp += sprintf(buffp, "\nbucket[%d] -> ", i); } while (!rht_is_a_nulls(pos)) { struct rhlist_head *list = container_of(pos, struct rhlist_head, rhead); - sprintf(buff, "%s[[", buff); + buffp += sprintf(buffp, "[["); do { pos = &list->rhead; list = rht_dereference(list->next, ht); p = rht_obj(ht, pos); - sprintf(buff, "%s val %d (tid=%d)%s", buff, p->value.id, p->value.tid, + buffp += sprintf(buffp, "val %d (tid=%d)%s", p->value.id, p->value.tid, list? ", " : " "); cnt++; } while (list); @@ -528,7 +529,7 @@ static unsigned int __init print_ht(struct rhltable *rhlt) next = !rht_is_a_nulls(pos) ? rht_dereference(pos->next, ht) : NULL; - sprintf(buff, "%s]]%s", buff, !rht_is_a_nulls(pos) ? " -> " : ""); + buffp += sprintf(buffp, "]]%s", !rht_is_a_nulls(pos) ? " -> " : ""); } } printk(KERN_ERR "\n---- ht: ----%s\n-------------\n", buff);