Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1j4GCk-0004BP-Dp for mharc-grub-devel@gnu.org; Tue, 18 Feb 2020 22:39:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42842) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j4GCh-0004BB-4h for grub-devel@gnu.org; Tue, 18 Feb 2020 22:39:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j4GCe-0001P7-Ie for grub-devel@gnu.org; Tue, 18 Feb 2020 22:39:14 -0500 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:35828) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j4GCe-0001LR-C5 for grub-devel@gnu.org; Tue, 18 Feb 2020 22:39:12 -0500 Received: by mail-qk1-x743.google.com with SMTP id v2so21824376qkj.2 for ; Tue, 18 Feb 2020 19:39:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=3s3+0HyWO1lpM6A/XFtw/xWv1qsfkz9mWKQ/k29vPTo=; b=JddDtSmxVkiqDVX8ku5t0oC/bJ4cNt7noeOkwK2waOI7yi0PhZk4c7xuUYdFtDpDlO rhP9yClrKnqJyplQGE5cO7CoqUk97AN2TREcMT/qpGgG0RSzMQd6pECP7tyH2ZItlj3Y Ed4Zfk3TsNgisyj3uFRMOJqRAMacFEfPfcNUk9Mg6BF/A9XaR1/sFgoQray2nnqbPm06 JeAJfsD2L/gr3TQgF/XXGpduR80yVzmYSS5OJuQBpnQgUTK/likHBwxCTutk2VpKDEhM 10MSt4sl0mEAy1n/j7/B2MOZMQhz2qoQagRDL5ODp0zbtglQZuVRjo56WKmy8QPgdVtI odeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=3s3+0HyWO1lpM6A/XFtw/xWv1qsfkz9mWKQ/k29vPTo=; b=MTiHmvqUsvHl5yNZhVi2oZRZUwPOSLOnlJUDqyfjTfu95b7tfoIl7MohuRM+Y3rXQF 0S3ioWB1S6Jn/toxw4+Nc6RVNMHECh4peD9RyU/aV09CPT+TNHls7RkIZXdeCdcO9ED9 5zwZlKRcQytKKO8yhZEkfV+rma1zdJwnzrPaneyvlVNzbpkAw6foqrhOt/YX/M6ijIqT Chsvtnv0lFQOVAguMTr3JF2Aqr1JnlQWeTrNzYerP8k4KyZ+4vj068De7+lvfWKmjMs+ DhwTATf+jFVu3zDg9yDNQQ2O00tRWqT/g2xfucn/MUsAhABvWO/2iuOsc3OWNem+4pXa PQYg== X-Gm-Message-State: APjAAAVduxYKZoDMq7F1rvBuRzgPcfAMXncsVXUMKtZm3Mk3cYdDLYIp uNNGzn2kXR1ML+FbQATWE4Mz9Rt6 X-Google-Smtp-Source: APXvYqziwJQennFrPh1l5ncR+mwyvZ5uxE1eKvxgeyq2TEwr7drf4IWEiEJJwgXUWJymNjJT6erbUQ== X-Received: by 2002:ae9:e10a:: with SMTP id g10mr21631126qkm.493.1582083549762; Tue, 18 Feb 2020 19:39:09 -0800 (PST) Received: from ?IPv6:2606:a000:111d:3fe:bd86:d757:664a:231b? (2606-a000-111d-03fe-bd86-d757-664a-231b.inf6.spectrum.com. [2606:a000:111d:3fe:bd86:d757:664a:231b]) by smtp.gmail.com with ESMTPSA id 65sm311861qtc.4.2020.02.18.19.39.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 18 Feb 2020 19:39:09 -0800 (PST) Subject: Re: [PATCH] Make grub_strtoul() "end" pointer have the right const params. To: grub-devel@gnu.org References: <20200204210206.1256169-1-pjones@redhat.com> <2f461a03-8bbd-e3b7-0bdf-390beb74d73b@gmail.com> <20200219003211.lfwfmcjce2hpndzb@redhat.com> From: Nicholas Vinson Message-ID: Date: Tue, 18 Feb 2020 22:39:07 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20200219003211.lfwfmcjce2hpndzb@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::743 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 19 Feb 2020 03:39:16 -0000 On 2/18/20 19:32, Peter Jones wrote: > On Tue, Feb 04, 2020 at 08:04:30PM -0500, Nicholas Vinson wrote: >> On 2/4/20 16:02, Peter Jones wrote: >>> grub_strtoul() and grub_strtoull() don't make the /pointer/ to "end" be >>> const like normal implementations do, and as a result, at many places in >> >> grub_strtoul() and grub_strtoull() appear to be patterned after the C >> standard functions strtoul() and strtoull(). >> >> According to C18, the signatures for those functions are: >> >> unsigned long int strtoul(const char * restrict nptr, >> char ** restrict endptr, int base); >> >> unsigned long long int strtoull(const char * restrict nptr, >> char ** restrict endptr, int base); > > It's true that the standard does not mandate any const usage on the > endptr parameter, and it does have restrict. I'm happy to add restrict, > though I'm going to ignore it for the rest of this email, because the > standard literally says it changes no semantics. Having "const" on both the > object pointed to by **endptr and on the outer pointer is still > compatible with the standard, though - nothing that's allowed by the > standard isn't allowed with those qualifiers. Semantically, the > behavior is described in a way that is compatible with making those > const as well: the value written to *endptr must point inside the object I'm afraid that's not true. That interpretation ignores the fact that the caller is permitted to use both nptr (provided the first argument is not const char *) and endptr to modify the underlying object. For example, the standard allows: char *num = "12345_"; char *end; unsigned long long l; l = strtoull(num, &end, 10); // returns 12,345. end points to '_' **endptr = '6'; l = strtoull(num, &end, 10); // returns 123,456. end points to '\0' With the proposed modifications all such use cases would require explicit casts to suppress the warning gcc would generate. > pointed to by nptr, and neither function is allowed to modify that > object. > > Adding the const requirement does give us benefits though: > - It means when someone is modifying grub_strtoul() or grub_strtoull(), > as inevitably happens every once in a while, making an error which > would violate those requirements will cause an error building that > change, rather than making it in to the tree. That is, "const char **" > protects us from making an error that writes modifies the object nptr > points at, and "char ** const" protects us from accidentally changing > the local pointer instead of the caller's pointer 'const char **' also requires the caller to cast away the const or use strange pointer arithmetic to modify the underlying object. 'char ** const' is probably OK semantically. > - Likewise, if someone isn't thinking through what they're doing and > takes the const out in order to "fix" some perceived problem, that > should alert whomever is reviewing the code. In both of these agreed. > regards, having the const qualifiers helps us trust that our > implementation is correct. > - It lets us pass in pointers that we *want* to be const from the > caller's point of view. It means that if a function calling strtoul() > has a "const char *" argument, it can safely pass that as nptr as well > as passing its address as endptr; without making the object const (i.e If you add the 'restrict' keyword, then endptr cannot point to any part of the object nptr points to. The standard C functions have never allowed this usage which is why 'restrict' was added to the declarations in C99. It's syntactically valid to do this with the GRUB variants (just like it is with C89), but it's not logically valid. > at least "const char ** endptr"), that'll get you a warning about > discarding the type qualifier. That applies equally to cases that > want to wrap these functions with another function that has similar > semantics; having const qualifiers allows that function to declare > those things const or not. Not having the qualifiers here means the > calling function also can't, which is where we wind up having a lot of > the ugly code. The wrapping functions can have all const parameters and still use the strto* functions without casting. Example below: long foo(const char *nptr, const char ** const endptr) { char *tmp long l; l = strtoul(nptr, &tmp, 10); *endptr = tmp; return l; } Are there functions in GRUB that attempt to do this? > - The same applies to cases where the object is (or should be) genuinely > immutable - for instance, data that's in .rodata, in data provided > by an option ROM, or firmware configuration table. It is better to be > able to keep the type qualifier throughout the whole stack. > - It allows the optimizer to make assumptions about how we're using it > which may allow it to generate more efficient output without having to > analyze the caller. > >> Therefore, I recommend the GRUB maintainers keep the existing signatures for >> grub_strtoul() and grub_strtoull() as they more closely follow the >> signatures of the standard functions. > > Obviously, I don't agree. > >> I also skimmed through the rest of the changes and it seems to me most of >> the misuse of these calls could be solved simply by doing some variation of: >> >> const char *ptr = ...; >> char *end; >> >> strtoul(ptr, &end, 10); >> ptr = end; > > While this is valid in many of the existing cases, it's not as good in > several ways. In addition to the last three points I made above, this > also creates an unnecessary alias into the character object, which the > compiler then has to track. In this case, since it is both a qualified > type of the same type of object and a character object, it doesn't > violate the aliasing rules ("-fstrict-aliasing -Wstrict-aliasing" isn't > allowed to complain), but it can cause the compiler to both take longer > and generate more, slower code than if that were: > > const char *ptr = ...; > unsigned long val; > > val = strtoul(ptr, &ptr, 10); > As I stated above this is not logically valid. > That's true for both the caller and in the implementation of strtoul() > itself - though I don't think with current GCC it *will* cause the > strtoul() output to be much different, because it's pretty simple, but > for a more complex function like sscanf() calling it, the chances are a > bit rougher. Hypothetically, it can also reduce the chances that it > winds up getting inlined if we were to use -flto. It's also not logically valid to use sscanf() that way either. The only standard C function I can think of where it is logically valid to have source and destination buffers overlap is memmove(). > >> which is what I would personally recommend instead. If the compiler >> issues an error or warning with the above, then that's a bug the >> compiler maintainers should fix. > > That's true, but it puts limits on the caller's usage and the compiler's > output >