[clug] Code question: Why doesn't this seqfault (kernel/printk.c)
Darren Freeman
daz111 at rsphysse.anu.edu.au
Mon Oct 20 12:32:16 EST 2003
On Mon, 2003-10-20 at 12:27, David Price wrote:
> On Mon, Oct 20, 2003 at 12:14:47PM +1000, Michael Still wrote:
> > if (p[0] != '<' || p[1] < '0' || p[1] > '7' || p[2] != '>') {
> > Which is a few lines in. Anyways, I'm now left wondering why the first if
> > statement doesn't cause a segmentation fault. As best as I can see, there
> > is no check to make sure that two characters after *p is in our memory
> > space.
>
> I may be wrong, but I think this is probably why it is okay:
>
> Since p[0] != 0, p[1] must be addressable. If p[1] is the null
> character, then the test p[1] < '0' will be true and the rest of the
> line will not be executed. If p[1] is not the null character then
only if gcc decides to execute left-to-right when testing expressions.
IIRC there is no guarantee in C that this is the case, although it's
often true.
> p[2] must be a valid index, so were okay.
>
> Hope I made sense,
> David
Darren
More information about the linux
mailing list