[TUHS] Maximum Array Sizes in 16 bit C

Leah Neukirchen leah at vuxu.org
Sat Sep 21 05:40:10 AEST 2024


Rich Salz <rich.salz at gmail.com> writes:

>>
>> if there need to be negative references in array accesses (which certainly
>> makes sense to me, on its face), it seems reasonable to have whatever
>> intermediate variable be signed.
>>
>
> In my first C programming job I saw the source to V7 grep which had a
> "foo[-2]" construct. It was a moment of enlightenment and another bit of
> K&R fell into place.  (
> https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/grep.c; search
> for "[-")

Now this thread already derailed into C undefined behavior semantics,
but nobody bothered to look at the actual code, which is perfectly fine:

	if ((c = *sp++) != '*')
		lastep = ep;
	switch (c) {
...
	case '[':
...
		neg = 0;
		if((c = *sp++) == '^') {
			neg = 1;
			c = *sp++;
		}
		cstart = sp;
		do {
...
			if (c=='-' && sp>cstart && *sp!=']') {
				for (c = sp[-2]; c<*sp; c++)
					ep[c>>3] |= bittab[c&07];
				sp++;
			}
			ep[c>>3] |= bittab[c&07];
		} while((c = *sp++) != ']');

Since sp has been incremented twice already, accessing sp[-2] is fine
in any case, but it's also guarded by cstart, so the regexp range
"[-z]" doesn't expand to [[-z].

-- 
Leah Neukirchen  <leah at vuxu.org>  https://leahneukirchen.org/


More information about the TUHS mailing list