From patchwork Mon Sep 21 08:19:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 40465 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59D823861841; Mon, 21 Sep 2020 08:20:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59D823861841 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600676445; bh=nk6Nz90d7elR9TgYvnl2ORkyDHBVHHDaKvkIQg44vSM=; 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=qYnsh9RNr7wYArI1YLisPQKpqJiOCbsXlmZc32NMzU7HUn9RwRtNpo7Wz85e9f54I 6cSISdxcXkutoDc6JbhFSJS/jjm2xUZa7vUi9N1xalV066mC840sDXVrIHOiLahatS F8W+e2tQIw39JJx6pmOm1/S7ojzyAl+zMHMiNVOE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id E64453857C5E for ; Mon, 21 Sep 2020 08:20:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E64453857C5E Received: by mail-wr1-x431.google.com with SMTP id w5so11719455wrp.8 for ; Mon, 21 Sep 2020 01:20:41 -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=nk6Nz90d7elR9TgYvnl2ORkyDHBVHHDaKvkIQg44vSM=; b=axxzL8JkfHa4O23RnVJnXxO8AbWqHx0rFwH5+xZd/1TQ1LNLWXA7MFXv0YbxLSJ93E CLxr0bmDXkoGKDe8x2QUXBoG6q4yw7+GpOJ3WItBSgdAZc6CORFCqG1mB8B8oeKJ3msr eKzXIY1PmTKfvnL8IFHzapFG6ZkVpSukVH0QE6Kfbk6DzqNhLielZRAxtBl6gYB+m1Ds MFMMCraZBaBXi9Qwae9AoBNjNbh62GbwdI0t+gYTxwrDiUj9TBOvN22c7+4yOsS+F9m8 VKcbd5Wfp83nvlL61AXmeQfK4NEj8j7bTEOgMCNWUPBX/OBpODOTDtOdt10C5PdfuUlH Wgwg== X-Gm-Message-State: AOAM5319oaelmK1KnXQr/AzDeaR3dB8gLGSlLwo/U4lSGu2c8reuZo7v UChGH/pp73TW0aitH2u+PFxo4ragpZc= X-Google-Smtp-Source: ABdhPJwu+A88Cq73TCzKnq7wHqQkzhCBUVI7LkM9MMjL471u0GqqrNx66VJDzlUuYXASae7/8xqJsA== X-Received: by 2002:adf:c44d:: with SMTP id a13mr50593118wrg.11.1600676440912; Mon, 21 Sep 2020 01:20:40 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id l5sm19102396wmf.10.2020.09.21.01.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2020 01:20:40 -0700 (PDT) To: mtk.manpages@gmail.com Subject: [PATCH v2] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example Date: Mon, 21 Sep 2020 10:19:34 +0200 Message-Id: <20200921081933.24196-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <61f4e2a4-d468-ceba-2ccf-ce0c061aa20b@gmail.com> References: <61f4e2a4-d468-ceba-2ccf-ce0c061aa20b@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 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, I added the part about range checking, and used a type with defined limits to show a complete example. Thanks, Alex man7/system_data_types.7 | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/man7/system_data_types.7 b/man7/system_data_types.7 index dd1d01aab..ba1338179 100644 --- a/man7/system_data_types.7 +++ b/man7/system_data_types.7 @@ -629,6 +629,68 @@ 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 a 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 actual 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 */ + if (tmp < -1 || tmp > 1000000) + exit(EXIT_FAILURE); + + /* Copy the value to the suseconds_t variable 'us' */ + us = tmp; + + /* 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)