From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.cs.ucla.edu (mail.cs.ucla.edu [131.179.128.66]) by sourceware.org (Postfix) with ESMTPS id B2C923858D3C for ; Fri, 10 Nov 2023 05:36:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B2C923858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B2C923858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=131.179.128.66 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699594606; cv=none; b=P8ssasK8XFBEf1ODj5HCnTst4qY+t1yEUuwWY/AGaGflZfxYXkjX4YKjDiMFcAwbWVRucB598Onv0YJlmCRQjBc6OJsWzTHivmcdbPwW2Nw/0g1tSg/KYtdkH7zrPATyAB9jT47F4f3ArH4PJg8IgOQW1HwsCKZ+ezDBpcAScnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699594606; c=relaxed/simple; bh=FB5huN4IcIVysHYWnIdQIpfk7qwMDMto6UrkfrK/ngA=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=iadmRQGd1QE/UZiBqXQjjdONh6R5md7zmLy2aQnv1mqSAY+0vfGNM3veWmv6SKPgiWfzhLeKAHzt5CCGeT1dIrSHw40I2weMG7F0MVeiW1DtiQkyB62jLWhQXfwGL4yKQRCA1P1kk4xUSPIS6AiHNXAz+RaPaGkLQna1Mbf7TR0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 821423C011BDE; Thu, 9 Nov 2023 21:36:44 -0800 (PST) Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id lGvPFZaas7tI; Thu, 9 Nov 2023 21:36:44 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.cs.ucla.edu (Postfix) with ESMTP id 4499C3C011BDF; Thu, 9 Nov 2023 21:36:44 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.cs.ucla.edu 4499C3C011BDF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.ucla.edu; s=9D0B346E-2AEB-11ED-9476-E14B719DCE6C; t=1699594604; bh=RLJ82Ex7eI8zb4+vCLFqA4LBb7ihr2VnJCC6mLxObbM=; h=Message-ID:Date:MIME-Version:To:From; b=oF2+5SEkzjSE6rlxwgeOyig9TiAGCzazICP/JGoD4GXEzxfzg0q6SO/UzifbBF50G HqNgsuFlrukT32Xbl+2nt+wIYa7ijztddUNPQqyW31Ct0oAazMc7RsyDzyeeBzN2Og duRIJ2mXk0gD0IT0utZDcFBY1Pqj8ZKIyJauwRqXvILeF5Z92XO5EmpNq4nEmWWsfu Cj+RZjCVp8GSnPWeJiwGhVRW4ZFIELpet/bKSSVfbe0TSRg11HmkUFyFO/7ArYmRIk 40pf6KzBEaJTkSHXOH1HkrhQhm9fhntlGBWLCTGwbEJ+GFhLLUkguLDwHTEpdqdfDj uR7DjzCa3f1MA== X-Virus-Scanned: amavisd-new at mail.cs.ucla.edu Received: from mail.cs.ucla.edu ([127.0.0.1]) by localhost (mail.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id TZXO-Usaoemr; Thu, 9 Nov 2023 21:36:44 -0800 (PST) Received: from [192.168.254.12] (unknown [47.148.192.211]) by mail.cs.ucla.edu (Postfix) with ESMTPSA id 0F9453C011BDE; Thu, 9 Nov 2023 21:36:44 -0800 (PST) Message-ID: Date: Thu, 9 Nov 2023 21:36:43 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: strncpy clarify result may not be null terminated Content-Language: en-US To: Alejandro Colomar Cc: Jonny Grant , Matthew House , linux-man , GNU C Library References: <20231108021240.176996-1-mattlloydhouse@gmail.com> <20231109031345.245703-1-mattlloydhouse@gmail.com> <250e0401-2eaa-461f-ae20-a7f44d0bc5ad@jguk.org> From: Paul Eggert Organization: UCLA Computer Science Department In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 2023-11-09 15:48, Alejandro Colomar wrote: > I'd then just use strlen(3)+strcpy(3), avoiding > strncpy(3). But that is vulnerable to the same denial-of-service attack that strlcpy is vulnerable to. You'd need strnlen+strcpy instead. The strncpy approach I suggested is simpler, and (though this doesn't matter much in practice) is typically significantly faster than strnlen+strcpy in the typical case where the destination is a small fixed-size buffer. Although strncpy is not a good design, it's often simpler or faster or safer than later "improvements".