Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4897843iob; Mon, 9 May 2022 04:22:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwi1yQlbt6B0/o3xWn1uRaPJEWNfl2PGjyHYwXyq5IJmEigeTUM6IxW7+/6m7JFTKVr/faL X-Received: by 2002:aa7:938c:0:b0:50d:418a:cdfb with SMTP id t12-20020aa7938c000000b0050d418acdfbmr15712756pfe.72.1652095334278; Mon, 09 May 2022 04:22:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652095334; cv=none; d=google.com; s=arc-20160816; b=ZQfTsZ+LlWqbtiu7lsMr1Ddco7AFNffwCbwgy+XpY/8AVWFA3u/ahug2OEQeqRwqjc tqTC3eRESxOL0VQ9AbEDiB1cqUliQ0a/n0DwPediM7+NpDTpWKXOY91kLRfsSWyjZeY0 YfyeKkcU2bvT/9ODh4ax7pid8uGNSkJd19rw+3VoN8y9wbLa4HWkzmyJLfBIQTNhuBWh V86uHiDAAhi1fWEzzUFS/Z16mehz0U8zhcnfllCMp7+am1LWObgRMV0foyse0lBECG8U 5541LBowvTl5gva7Dv4fcbkeyQEZEqr1LYxQ8ATUB9rTOTw+NPO4ZJGwhyo+8zBX9dHV BHlQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5YipGohanjl3hZ3T7PVps89s+JZj0KDjWa2cNf+YisY=; b=L+yq53t45Ap1qUtSgqnM3ISX+Mk9p41fdMpjy8PSFuAwuCcnx5RsjtasuxCgw6f3Vd 8N37N5qLY0fq5H7P7VmChhCNoUxz0uJM6wHcrUa2354Id49Vyi6WRLOMVXWkWhjovZpw TmaMUKAy/yX+5ucZbPmQlxo8QeCKBR13oxBGVRO/48gqdBGaZPMspK2/tF1wpoS61NmP y3ZEPfjP1Oscc2oXmmj32P3g0H2ezNxsMxJtMsfCPIErHxT8Mlu6kA+mVV0rPyFuzR3r d5/RZ/xZP0ki5ExpqO4PoYFYrun7icxrr4Q5mu9TPAUSl3DXXlujC6A2Y99CQH1PzcAp wE6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=WHfbcEyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id g23-20020a63dd57000000b0039cf81e07d1si13167029pgj.451.2022.05.09.04.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 04:22:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=WHfbcEyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8A7D72A83E0; Mon, 9 May 2022 03:31:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235849AbiEIHGU (ORCPT + 99 others); Mon, 9 May 2022 03:06:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234946AbiEIG6q (ORCPT ); Mon, 9 May 2022 02:58:46 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 459C815E615 for ; Sun, 8 May 2022 23:54:54 -0700 (PDT) Received: from localhost.localdomain (unknown [138.197.159.143]) by gnuweeb.org (Postfix) with ESMTPSA id 647107E7F6; Mon, 9 May 2022 06:54:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652079293; bh=S0U9M32154HScyfLSnF1HZ9qZvAR0EcCgcUUKQmmE44=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WHfbcEyoxuzZ2fSeGRnGn8fybzzOrakqv0xyTMDpZZADLUDgiaQ5U02yjbXyKtgV+ IkWPjQvCgsAOUuuQ7J2zbiPNvpxcLTZ/4BWH/81NNxvhWGc2TJhYfi10JS74vBa9BC 303lJS1TaaNn4Rh512aK20HkxZgVLy/+RjWzhnOlYxJspgX06Bb4yclmPg/nBA5/+w 5Yr8o8o3jAWurcDjuyrFgQjC3n811L4WZmdzEPqlUErAPppZF+qZaavLhsyHn1F6P8 sesXfFJujp1R0DbYiZJmJ7XTwesHLxywUqLkUTTNW6zVnhB3Jj6LeUseHuMVg0XkU+ EFzkaxsQsz+yQ== From: Alviro Iskandar Setiawan To: Ammar Faizi Cc: GNU/Weeb Mailing List , Linux Kernel Mailing List , Alviro Iskandar Setiawan Subject: [PATCH 1/1] tools/nolibc/stdio: Add format attribute to enable printf warnings Date: Mon, 9 May 2022 06:54:45 +0000 Message-Id: <20220509065445.3912334-2-alviro.iskandar@gnuweeb.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220509065445.3912334-1-alviro.iskandar@gnuweeb.org> References: <20220509065445.3912334-1-alviro.iskandar@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we use printf and fprintf functions from nolibc, we don't get any warning from the compiler if we have wrong arguments, for example the following calls will compile silently: ``` printf("%s %s\n", "aaa"); fprintf(stdout, "%s %s\n", "xxx", 1); ``` Those calls are undefined behavior. We can catch it at compile time by adding format attribute to those function declarations. After this patch, we get the following warnings: ``` warning: format `%s` expects a matching `char *` argument [-Wformat=] warning: format `%s` expects argument of type `char *`, but argument 4 has type `int` [-Wformat=] ``` Signed-off-by: Alviro Iskandar Setiawan --- tools/include/nolibc/stdio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 15dedf8d0902..009dd6ae68f2 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -273,7 +273,7 @@ int vfprintf(FILE *stream, const char *fmt, va_list args) return written; } -static __attribute__((unused)) +static __attribute__((unused)) __attribute__((format(printf, 2, 3))) int fprintf(FILE *stream, const char *fmt, ...) { va_list args; @@ -285,7 +285,7 @@ int fprintf(FILE *stream, const char *fmt, ...) return ret; } -static __attribute__((unused)) +static __attribute__((unused)) __attribute__((format(printf, 1, 2))) int printf(const char *fmt, ...) { va_list args; -- Alviro Iskandar Setiawan