From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 4FC523858D1E for ; Fri, 10 Nov 2023 11:06:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4FC523858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=jguk.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jguk.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4FC523858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699614410; cv=none; b=Yy0DjqRoIgeNlv3YEJJym3bg5VPYW3wstDDntCGpCCnr0IHLBGRZrX/fiAaxlOQJyNP0vz1ezbQylME4jARtXtXs+TbmC7zeylYLL6fshIb2bxTSUb+IZYxRQIfsVCSNqzcnmuwRNcYYH3/Gi1e6/ueOWnNJmPmwN33N/YzZqM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699614410; c=relaxed/simple; bh=BVcUwAcwWPtRP/RvAfmCa630Ba0z/fvX7K/HJ4TNRpU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=WvJ8pE6smpIZhJfo/QsTlSsOdaMTr1hmwBI1vMSkSKzXVz6LSVfOmerYQM7yXhmaao1i9fG4Pz86g/bkgh3V/YoehqSZkfLLff97tzrTcCGzOa4LbdFHURbxuzGqDiJ80CfY9ngR05/yRm1YFUnABfbRUvX8sO7zlpGctiqXogw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-32da9ef390fso1127168f8f.2 for ; Fri, 10 Nov 2023 03:06:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jguk.org; s=google; t=1699614407; x=1700219207; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=D5fxKb0h6GfVCcvhlT/egQ3j3WAvxElS/dene/XITCQ=; b=ltGkPLUBpSF+XpGhfMJ49HqJR49/ncr3pcbrMJHGCnb3xchNtBsJKDCyiKy858bbJs HSmKI5s4otp9k4Zuev58CoVs7KJpjH1s9e7B+ACHMl/dlfUrXypzHDfreYCWTra1QW20 PvRMZ/YCFSrf6Da8e87BFRe+cJna4sA0q75DBQ3VLHTPaUZM1Ag02u9PE8k3jNcgEnlV tAZAM4MlC95/4/ev+jx4znE/EhzfvqiiFXeDgVi+twfHryTHaHSacJKGD6cBnopGFha+ fcrac01cdTS9Au+8Tz/QoQreAgkZ6S5kTDt+fAgEuc5AbxTZZmU5VkKngx3aPYGq8FEG dvfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699614407; x=1700219207; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D5fxKb0h6GfVCcvhlT/egQ3j3WAvxElS/dene/XITCQ=; b=Bz8nQBvHDVDshwfU3AuXGQ3/k9gnTiXHxum4PQ7tfUl7sEgPrg9wp7IQYxYPV93se0 7PTjOFB709Kpv9d/We5YEFMSX41ZwbTP/XaSeMaJMQMjQkRKkg5Pib9bWOosBl/+qhcW kQdUcC32/8E/DZolaFk40/LyccSLWvs/DIIsWyLw3llswHJszVYxB78boLNJrurGiG/0 /n17ecO4UWNqigLrljUb2FLB0kp9Cx4zUePiUxvYNrdJcaWYij5lLxTKXlM86VwANbZH Pb7CmefDrTG+5EWNVPg2WGWIT8vgaWhiA2+L6X5caNZz+IrhgpkWq17mhXVypojgLjLB vYTw== X-Gm-Message-State: AOJu0Yzdlm5JQG87xasLs8eR6/jDTv0h7qSX6WrsN987rUKHy1fcdl9Y ALXg+i/FeDhe/X0AWTtTJzSgYg== X-Google-Smtp-Source: AGHT+IHMfnaJKbRPnYfaf2oWVZ1i+lySMn6mA6mw+NYzqyC5sHGvmFpfQIFn5DsoxT5oAyxB51AAhQ== X-Received: by 2002:adf:f84d:0:b0:32d:a4c9:8ab4 with SMTP id d13-20020adff84d000000b0032da4c98ab4mr5349834wrq.65.1699614406827; Fri, 10 Nov 2023 03:06:46 -0800 (PST) Received: from [192.168.0.12] (cpc87345-slou4-2-0-cust172.17-4.cable.virginm.net. [81.101.252.173]) by smtp.gmail.com with ESMTPSA id h8-20020adffa88000000b0032d9337e7d1sm1682034wrr.11.2023.11.10.03.06.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Nov 2023 03:06:46 -0800 (PST) Message-ID: <766ffee7-bd3d-4463-9b68-0c4e0814419f@jguk.org> Date: Fri, 10 Nov 2023 11:06:45 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: strncpy clarify result may not be null terminated Content-Language: en-GB To: Stefan Puiu , Alejandro Colomar Cc: Matthew House , linux-man , GNU C Library References: <20231108021240.176996-1-mattlloydhouse@gmail.com> From: Jonny Grant In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 10/11/2023 10:40, Stefan Puiu wrote: > Hi Alex, > > On Wed, Nov 8, 2023 at 9:33 PM Alejandro Colomar wrote: > [.....] >> strncpy(3): >> CAVEATS >> The name of these functions is confusing. These functions produce a >> null‐padded character sequence, not a string (see string_copying(7)). > > I'm a bit confused by this distinction. Isn't a null-padded sequence > technically also null-terminated? If there's a '0' at the end, then > it's a string, in my understanding. Or was the intention to say "a > character sequence that may be null-padded", where the case in which > there's no padding at all being the reason for the distinction? This is a null padded sequence of characters in an array: char buf[4] = {'a', '\0', '\0', '\0'}; I'm sure we are all well aware from this long email thread, strncpy is designed to fill fixed sized arrays, and pad with NUL bytes '\0' if any space left. Otherwise, the array buffer is left not padded.. there in lies the trouble, a possibly not terminated sequence of characters. Someone thought saving the extra byte was a good idea. It would have been better if that programmer had crafted their own local function rather than put out the strncpy function which is similarly named to strcpy(), they could have called it copy_to_array_nul_pad(). // a not terminated array - using printf, or strlen will carry on reading off down the memory until it finds a NUL byte '\0', perhaps reading out side the addressable space of the process, causing a SEGV. char buf[4] = {'a', 'b', 'c', 'd'}; Hope that helps. Kind regards, Jonny