Received: by 10.213.65.68 with SMTP id h4csp348042imn; Tue, 13 Mar 2018 06:24:29 -0700 (PDT) X-Google-Smtp-Source: AG47ELt/iVghGz4k6aLT5bo510DS3HDOHkWJJNmEK/D7jeaNZRKrSr9r82xlL9Uz8nd7NZG6v4be X-Received: by 2002:a17:902:5a44:: with SMTP id f4-v6mr590770plm.116.1520947469234; Tue, 13 Mar 2018 06:24:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520947469; cv=none; d=google.com; s=arc-20160816; b=qdQq07LMyQ8cWLWTfK+JRb5D4nF9LqteZ2NBJVfF6rR3twXYlNUv9Vlo2V/pJE8Fwm Ltqd905oLNhFUdi7co2hJmrCIezkB/XeYHXiYw0d7+EeM9PuziTkK2ypIgUbu0cuwa/O UmVqONz9AV7D3txepd7OlxLdNfv5xrTv2tK4Ir9u9XBhLI4tmR9fqkbbyAjOofya1MIu 17Z5/Lhxcwc6VOJYl7Abipn177fUtaK3gEZRPsQvYXYK4nK5EbB8KxeFZoaUSbu8pvaH 78NSV46Y/COxYEnNJYF0jhLuAYb+SgnfjUpwyujqkITgdBpvcp9pDsWo56eKRkpJdT0N YN3A== 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 w8-v6si124507plq.641.2018.03.13.06.24.15; Tue, 13 Mar 2018 06:24:29 -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 S1752731AbeCMNWo (ORCPT + 99 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.203220-1-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 X-Provags-ID: V03:K0:1zKOjzsUv79byOvmpf64nc+Ek6WuDIX6rI/9E45P4+hpLloQk2K Ys3FuRkxQn59yWOuNnTe3gT0ReJEff5Hbz1pNAfy85bmwT7o790bG6X4jO38FFtYLjPoPLw 0NL8BwjF4oZRTMB3Lva2HFfXJLdeJGyzBVOcIbIncCOh25bxJUuqCM2LukEWtWUg+WomTKR Bqp0l6HY4TkKX6ErwleAw== X-UI-Out-Filterresults: notjunk:1;V01:K0:tmR+yglkwmc=:UruXE+MAfKYkRSG7bGZuoF 82Jr2ojl4WuRBH0vcyBg2x5bw0ZcvkFtKmzfI68u+dtPE9IU/ud7fL6oRLqapFeyzS6jYafh5 T/NAKUEH1C3PG8GwPwyOPuC8iDVcsUPN/V/1J6DSp4nsV1V04aWr3EBwdlC/f9oogN+whZbN0 D3Ej2MqhCsk8DoTv5HwqhVpn7HMIZ9U2Tml3vh21wXocrIDpUs6SlCOYHseZyAT6OC7xsisBB DTlvZZ2IwnOjDNYZY+oop14kCRmtpp7Rqq9fr1B7Kz6pVysdOxYZhOmG89MZ7hVpWTT7s7Zle XCobmIurGzULMWYSn3aOTA/6xNeXQqPAp1x4OmECqxXtuFc2/+PoM4qzzliRNYEU/R/xeLYrs r3vXvAQBBJvDTHYfOkiqoQOnuJW5KCFSuVckPz/zR+bo32v+dxbjrNsnh4nQejIYP/R0zj0mY lGm5kY11ssV0Rb7Ijrsl5/ioEzLla6BUa/+Tg4CyCT4LJE6yoEDmSy6IePb2EfyTo7eq+sY6c ngVef1Iz8PbKaQ7aZ/dV8/mONtTWLDz6S2yfl62v+su9RKdaDbDGZIHJOFnXBQZ8FtBOp2COO EA7WGZ6eImp1xBm6jCQBX9c+Jl3Hy3WY6+tYXJdSxGXRs3Kkp/tx6dpIe18bIRmrwMPyODcVp 8XL9eU3geKyWbOZRJY4MfaFM+pOsNGNWmeTwW6iSEDIoIm1wJ/YIR+pZdJ10MBO2RSWQySMIU 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(-) 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); -- 2.9.0