[clug] Code question: Why doesn't this seqfault (kernel/printk.c)

David Price david.price at anu.edu.au
Mon Oct 20 12:27:00 EST 2003


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
p[2] must be a valid index, so were okay.

Hope I made sense,
David



More information about the linux mailing list