Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Memory protection between compilation units? Date: Tue, 01 Jul 2025 09:54:36 -0700 Organization: A noiseless patient Spider Lines: 30 Message-ID: <86frffq2b7.fsf@linuxsc.com> References: <20250611153239.6bc43323@mateusz> <86wm9hp0u2.fsf@linuxsc.com> <20250613085927.7b7cb344@mateusz> <86o6urp6b5.fsf@linuxsc.com> <102ksmk$evka$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Tue, 01 Jul 2025 18:54:40 +0200 (CEST) Injection-Info: dont-email.me; posting-host="9e46744e4985b53c2fe54e514d0912ff"; logging-data="3110354"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CNIHy5GmEudjJ2wXvPlLbWIdg4updaWo=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:KNPh1dVFzperMwqWuclRbFuQ2BY= sha1:cpVg8SUWFw22oo7VSPeqD9vNIiA= Xref: csiph.com comp.lang.c:393957 Mateusz Viste writes: > On 14.06.2025 01:31, Tim Rentsch wrote: > >> It isn't wrong to think of bitwise-and as masking-in (or possibly >> masking-out) of certain bits, but it still isn't a modulo. A >> modulo operation is what is desired; > > By "different viewpoints," I meant that while you approach the > problem by applying a modulo operation to the index so it fits the > array size, I tend to think in terms of ensuring the index > correctly maps to a location within an n-bit address space. > Naturally, the array should accommodate the maximum possible index > for the given address space, and that?s where the original code > fell short. And you're absolutely right that hardcoded values are > problematic, the size of the array should have been linked with > the n-bits address space expectation. I understand what you're doing. However one thinks of it, what is needed is a way to ensure the produced index value is in the range of array index values, and that the mapping covers the full range of array index values. Using bitwise-and is a way of solving a less general problem. Unfortunately: one, although it is known that using bitwise-and works only for certain array sizes, there was no check or assertion in the code to verify that requirement; two, it's a holdover from earlier times when the performance difference might matter, but now it's a premature optimization (and in most cases does not result in any improvement); and three, in this case using bitwise-and contributed to the bug, which wouldn't have happened if modulo had been used instead.