Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is it possible C is the security problem? Date: Wed, 04 Nov 2015 19:37:51 -0800 Organization: None to speak of Lines: 28 Message-ID: References: <660e1300-4ef9-48a2-a170-86372552b4d6@googlegroups.com> <5985bb23-835e-4655-8f60-da3c6a99aa71@googlegroups.com> <8fc4438a-bdf9-4dd6-977c-0cb0d28e64c2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="14303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zReFATHH/GJmCMv1u9tfY" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:FJ3cgQoqj+KDuQFU4CVBDLNcr7M= sha1:9JOnRV5wZbykzIXF16lN42J+Kss= Xref: csiph.com comp.lang.c:74919 supercat@casperkitty.com writes: > On Wednesday, November 4, 2015 at 6:40:07 PM UTC-6, Richard Heathfield wrote: >> It is simply *not possible* for that code to overstep the bounds of the >> array, and yet the overhead of bounds checking will be incurred, yes? >> (If you disagree, I'd be fascinated to hear why.) > > Compilers which bundle their own getc() routine may be able to determine the > range of values it can produce. Those which rely upon a getc() routine taken > from elsewhere should expect that it might behave in "interesting" fashion > and be prepared for that possibility. A programmer who's concerned about > speed could write `++freq[(unsigned char)ch]`, in which case compilers would > be able to recognize that the result was guaranteed to be in bounds. The behavior of getc() is defined by the standard library, so a compiler may assume that it behaves as specified (i.e., that it returns a value that either is equal to EOF or is in the range 0..UCHAR_MAX). Replacing getc() by a function that doesn't behave as the standard requires causes undefined behavior if it's done by the programmer, or non-conformance if it's done by the implementation. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"