Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8906860ybi; Tue, 23 Jul 2019 17:49:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqwedOS5wx0zamjO9tqTmtgMepBNbtsDns0N1aNzxUPLKeFTvuj9y8tW2TETvLaxo/FoCIac X-Received: by 2002:a65:6294:: with SMTP id f20mr80984414pgv.349.1563929398639; Tue, 23 Jul 2019 17:49:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563929398; cv=none; d=google.com; s=arc-20160816; b=xlszNDzeNmOHL6LlM94bmWr/RVJfVZZWTXFTRVowEKx4DluL1T6hPPWE+iUKt9J6My RWBv45B+6G048jJCNTbr+HsS3x5EUy3ZyOphb6sTi/9tDaaUGQ65EyxCqKI2+pxYs4Hc up9cUrCUnEKr36S3rsXUS+s8l8NaXzNqL3CxZEs+qRVi6RPB8OTwnZvnN4/e7vKnRSmQ P9nL/u//5FLvmUX+sKT/wpBZOeKfnX4S0Ls/Lc8ETrHY01K3PFJLbzzggQ6RtL2MZu4N CqrgGmn7ngGfKc4lMHK1tuFxT/VX+WlO+HjHAjICATjns4WBUzYVVLK2RmYy29+kIHrv eN4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=WZY5PFdn+z8eEbFfSxFC+m6xjbG/okyeR7I4e5unjMo=; b=wsYP105YjCGW/NrWcshP4IUipccrEUd4tOzu/rmkyVOjesDn6AS37youDwWivLLi5/ YPpe02Zw5ENyqUmRX1JuZ/joMLLZlinE98X37kh+35rQhuF4VJ+M9lx3tfQq/YzmdRIh ZWAAUx7q9xDXHpMhYrnm+5VIuHR5C0sE55HFswWzojqwlCGbQn3Q2jZRn870igdGmJFP FlXH0iuX+r0BqFafH/KPoUlRaxGx3wzjBThrToDeAJqvrL2oz2L1igLNNqB9lKDReKGC VZC4HarSdqFvc2s6q62j+hGkAj5Ezwv81dOIT1FCXdeFJNTTm+hiI4itGdm9T4zh6wE9 WGHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v33si44706061pgk.152.2019.07.23.17.49.42; Tue, 23 Jul 2019 17:49:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732002AbfGWPld convert rfc822-to-8bit (ORCPT + 99 others); Tue, 23 Jul 2019 11:41:33 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([146.101.78.151]:34687 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726410AbfGWPlc (ORCPT ); Tue, 23 Jul 2019 11:41:32 -0400 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-187-32j5YcqrN5ShbLouelXgfQ-1; Tue, 23 Jul 2019 16:41:28 +0100 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b::d117) by AcuMS.aculab.com (fd9f:af1c:a25b::d117) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Tue, 23 Jul 2019 16:41:27 +0100 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Tue, 23 Jul 2019 16:41:27 +0100 From: David Laight To: 'Rasmus Villemoes' , Joe Perches , Linus Torvalds , "linux-kernel@vger.kernel.org" CC: Jonathan Corbet , Stephen Kitt , Kees Cook , Nitin Gote , "jannh@google.com" , "kernel-hardening@lists.openwall.com" , Andrew Morton Subject: RE: [PATCH 1/2] string: Add stracpy and stracpy_pad mechanisms Thread-Topic: [PATCH 1/2] string: Add stracpy and stracpy_pad mechanisms Thread-Index: AQHVQSOiPIn99bSetkamp+/6YQTlwKbYVhaA Date: Tue, 23 Jul 2019 15:41:27 +0000 Message-ID: <5ffdbf4f87054b47a2daf23a6afabecf@AcuMS.aculab.com> References: <7ab8957eaf9b0931a59eff6e2bd8c5169f2f6c41.1563841972.git.joe@perches.com> In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-MC-Unique: 32j5YcqrN5ShbLouelXgfQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rasmus Villemoes > Sent: 23 July 2019 07:56 ... > > +/** > > + * stracpy - Copy a C-string into an array of char > > + * @to: Where to copy the string, must be an array of char and not a pointer > > + * @from: String to copy, may be a pointer or const char array > > + * > > + * Helper for strscpy. > > + * Copies a maximum of sizeof(@to) bytes of @from with %NUL termination. > > + * > > + * Returns: > > + * * The number of characters copied (not including the trailing %NUL) > > + * * -E2BIG if @to is a zero size array. > > Well, yes, but more importantly and generally: -E2BIG if the copy > including %NUL didn't fit. [The zero size array thing could be made into > a build bug for these stra* variants if one thinks that might actually > occur in real code.] Probably better is to return the size of the destination if the copy didn't fit (zero if the buffer is zero length). This allows code to do repeated: offset += str*cpy(buf + offset, src, sizeof buf - offset); and do a final check for overflow after all the copies. The same is true for a snprintf()like function David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)