From patchwork Mon Sep 21 13:32:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1368363 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Slv9t7R0; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bw55J47cjz9sV6 for ; Mon, 21 Sep 2020 23:34:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13BAB3850421; Mon, 21 Sep 2020 13:34:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 13BAB3850421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600695266; bh=Nq5YsyTwEt6MAvEkQS68agi8AmKwYFZipwQ1PiSteO4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Slv9t7R0U0D3/KZfzzuDfScxJLLEOOoZbxQRNSfm/BGZjS2Gu5BOAKLrn5Xj/Szcn 08rg3PwaEAxxoVZqSz76E8aFWnry00MRkpcVXOT5I0c/FbDu27ozYbmQPVpbIgamAh J0H/QaITYvJ0HGuIxSUfU5FyZ/ff+y6QvLCI/BVc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 1F5923858D35 for ; Mon, 21 Sep 2020 13:34:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1F5923858D35 Received: by mail-wr1-x434.google.com with SMTP id e16so12811534wrm.2 for ; Mon, 21 Sep 2020 06:34:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Nq5YsyTwEt6MAvEkQS68agi8AmKwYFZipwQ1PiSteO4=; b=m+y1g308BHb5Y+Ezv5S5xaD13eEIY4QCBuVu+6ep3WdQyjdoK3a8dx6XTJku5pmTYs LxhgLq8rauUdlj8VyrcFAEmddnmv0n5fbvX96k4LbBe9u1LsJY9/4F8jVNjZJ4PfAssQ Dzh7JsU6KwDIYnDGfGEz8saNE4FktV0XuHi5p7+vOSnxzlE/EolU1rUsIguLKPp+u1sa kSfZPX8FfcDKixHfDpCg31lTHpQi6RS6Vor8N3FV0+FS5A+aBJiFr/2q96FOGpyWyx5W TMPUPJN4LP048hTc/tzl16K3vcWNhe929XIT8F1ebuRz3XTZWQyCJDAShHGUuykQA2xJ ZzUw== X-Gm-Message-State: AOAM531NixOxFFaI5PcOYKbGab4SiVzDy2TdsXNdK/6QXY7oq1ziwKi7 3pfFxfFpYsbd0g71PYbnKIw= X-Google-Smtp-Source: ABdhPJzuG4sbCbU9cKBX/e0YMj2/OmsKDdioHJvWPeDjztVgR0QxDO6BOKTGRgS6A6e5kUOXf4IpgQ== X-Received: by 2002:adf:81e6:: with SMTP id 93mr52240103wra.412.1600695262172; Mon, 21 Sep 2020 06:34:22 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id 185sm21302512wma.18.2020.09.21.06.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2020 06:34:21 -0700 (PDT) To: mtk.manpages@gmail.com Subject: [PATCH v3] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example Date: Mon, 21 Sep 2020 15:32:57 +0200 Message-Id: <20200921133256.45115-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alejandro Colomar via Libc-alpha From: Alejandro Colomar Reply-To: Alejandro Colomar Cc: fweimer@redhat.com, linux-man@vger.kernel.org, libc-alpha@sourceware.org, Alejandro Colomar Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Reported-by: Michael Kerrisk Signed-off-by: Alejandro Colomar --- Hi Michael, wfix + I thought that checking between 0 and 1M might create confusion, so I kept that check, and added another one to differentiate the error code from normal values. Cheers, Alex man7/system_data_types.7 | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/man7/system_data_types.7 b/man7/system_data_types.7 index dd1d01aab..da57deffa 100644 --- a/man7/system_data_types.7 +++ b/man7/system_data_types.7 @@ -629,6 +629,79 @@ See also: .SH NOTES The structures described in this manual page shall contain, at least, the members shown in their definition, in no particular order. +.PP +Most of the integer types described in this page don't have +a corresponding length modifier for the +.BR printf (3) +and the +.BR scanf (3) +families of functions. +To print a value of an integer type that doesn't have a length modifier, +it should be converted to +.I intmax_t +or +.I uintmax_t +by an explicit cast. +To scan into a variable of an integer type +that doesn't have a length modifier, +an intermediate temporary variable of type +.I intmax_t +or +.I uintmax_t +should be used. +When copying from the temporary variable to the destination variable, +the value could overflow. +If POSIX provides lower and upper limits to the type, +the user should check that the value is within those limits, +before actually copying the value. +The example below shows how these conversions should be done. +.SH EXAMPLES +The program shown below scans from a string and prints a value stored in +a variable of an integer type that doesn't have a length modifier. +The appropriate conversions from and to +.IR intmax_t , +and the appropriate range checkings, +are used as explained in the notes section above: +.PP +.EX +#include +#include +#include +#include + + +int +main (void) +{ + static const char *const str = "500000 us in half a second"; + suseconds_t us; + intmax_t tmp; + + /* Scan the number from the string into the temporary variable */ + sscanf(str, "%jd", &tmp); + + /* Check that the value is within the valid range of suseconds_t */ + if (tmp < -1 || tmp > 1000000) { + fprintf(stderr, "Scaned value might overflow!\en"); + exit(EXIT_FAILURE); + } + + /* Copy the value to the suseconds_t variable 'us' */ + us = tmp; + + /* Even though suseconds_t can hold the value -1, + it represents an error code */ + if (us < 0) { + fprintf(stderr, "Scanned an error code!\en"); + exit(EXIT_FAILURE); + } + + /* Print the value */ + printf("There are %jd us in half a second.\en", (intmax_t) us); + + exit(EXIT_SUCCESS); +} +.EE .SH SEE ALSO .BR feature_test_macros (7), .BR standards (7)