Article View: comp.arch
Article #23Re: Optimization by programmers
From: EricP
Date: Thu, 08 Oct 2026 16:05
Date: Thu, 08 Oct 2026 16:05
68 lines
3728 bytes
3728 bytes
On 2026-Oct-07 15:25, Michael S wrote: > On Mon, 5 Oct 2026 11:36:59 -0000 (UTC) > cross@spitfire.i.gajendra.net (Dan Cross) wrote: > >> In article <86h5j5pky9.fsf@linuxsc.com>, >> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >>> Terje Mathisen <terje.mathisen@tmsw.no> writes: >>> >>>> Tim Rentsch wrote: >>>> >>>>> Terje Mathisen <terje.mathisen@tmsw.no> writes: >>>>> >>>>>> Tim Rentsch wrote: >>> [...] >>>>>>> I favor a type equivalent to size_t for use as array index >>>>>>> variables. >>>>>> >>>>>> In Rust, the only acceptable array index is of type "usize", >>>>>> which is effectively the same as u64 on most platforms these >>>>>> days, but does not need to be so: On a 32-bit target, it would >>>>>> be the same as u32. >>>>>> >>>>>> So effectively very similar to size_t. >>>>> >>>>> Maybe that limitation works okay in Rust. In C sometimes it's >>>>> important to allow a signed value for indexing. Probably 99% of >>>>> the time size_t is good, but not always. >>>> >>>> The fact that array indices are unsigned is one of the more crucial >>>> underpinnings of Rust safety. Even more importantly, all such >>>> indices are verified against the actual array upper boundary. >>>> [...] >>> >>> It's nice that array index values are checked against the array >>> bounds. >> >> Terje was actually incorrect here; bounds checks are added in >> debug mode, but are (usually) removed in optimized builds. >> > > Are you sure? > Methinks The Book states otherwise: > https://doc.rust-lang.org/book/ch03-02-data-types.html?highlight=array#invalid-array-element-access The reference is less see-spot-run https://doc.rust-lang.org/stable/reference/ Looking at the rules for type casting: https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#numeric-cast Some initial thoughts: There are no value range checks between cast types, just bit-wise reinterpretation to the new type with possible sign/zero extension or truncation for size change. They don't define the order in which those two operations, type and size change, occur. If one casts an i8 into a u16 it doesn't say whether the value is sign extended to i16 first then type changed to u16, or type changed to u8 first then zero extended to u16. Since an array index is defined as type usize, if the index expression is signed then it will be bit-wise reinterpreted as unsigned, and possibly turn a negative value into a large positive usize value. If bounds checks are disabled for production builds then you get exactly the same array overflow as in C.
Technical Details
Message-ID:
<jbPxS.600$MBQb.166@fx08.iad>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!s1-2.netnews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx08.iad.POSTED!not-for-mailReferences:
<1177fdi$26ea4$1@dont-email.me> <86bj9hr91x.fsf@linuxsc.com> <119dne7$2baig$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!s1-2.netnews.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx08.iad.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Optimization by programmers Newsgroups: comp.arch References: <1177fdi$26ea4$1@dont-email.me> <86bj9hr91x.fsf@linuxsc.com> <119dne7$2baig$1@dont-email.me> <86h5j5pky9.fsf@linuxsc.com> <11a024r$f52$1@reader2.panix.com> <20261007222539.000033d0@yahoo.com> Content-Language: en-CA From: EricP <ThatWouldBeTelling@thevillage.com> In-Reply-To: <20261007222539.000033d0@yahoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 68 Message-ID: <jbPxS.600$MBQb.166@fx08.iad> X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Thu, 08 Oct 2026 16:05:35 UTC Date: Thu, 8 Oct 2026 12:05:31 -0400 X-Received-Bytes: 3410