Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C char array overflow, okay practice?

Tags:

c

I'm going through the K & R book and the answer to one of the exercises is troubling me.

In the solutions manual, exercise 1-22 declares a char array:

#define MAXCOL 10
char line[MAXCOL];

so my understanding is that in C arrays go from 0 ... n-1. If that's the case then the above declaration should allocate memory for a char array of length 10 starting with 0 and ending with 9. More to the point line[10] is out of bounds according to my understanding? A function in the sample program is eventually passed a integer value pos that is equal to 10 and the following comparison takes place:

int findblnk(int pos) {
while(pos > 0 && line[pos] != ' ')
        --pos;
    if (pos == 0)                   //no blanks in line ?
        return MAXCOL;
    else                        //at least one blank
        return pos+1;               //position after blank
}

If pos is 10 and line[] is only of length 10, then isn't line[pos] out of bounds for the array?

Is it okay to make comparisons this way in C, or could this potentially lead to a segmentation fault? I am sure the solutions manual is right this just really confused me. Also I can post the entire program if necessary. Thanks!

Thanks for the speedy and very helpful responses, I guess it is definitely a bug then. It is called through the following branch:

else if (++pos >= MAXCOL) {
            pos = findblnk(pos);
            printl(pos);
            pos = newpos(pos);
        }

MAXCOL is defined as 10 as stated above. So for this branch findblnk(pos) pos would be passed 10 as a minimum.

Do you think the solution manual for K & R is worth going through or is it known for having buggy code examples?

like image 325
user2360745 Avatar asked Oct 22 '13 02:10

user2360745


2 Answers

If pos is indeed 10 then it would be an out of bounds access and accessing an array out of bounds is undefined behavior and therefore anything can happen even a program that appears to work properly at the time, the results are unreliable. The draft C99 standard Annex J.2 undefined behavior contains the follows bullet:

An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).

I don't have a copy of K&R handy but the errata does not list anything for this problem. My best guess is the condition should < instead of >=.

like image 89
Shafik Yaghmour Avatar answered Sep 22 '22 02:09

Shafik Yaghmour


It is never, ever okay to over-run the bounds of an array in C. (Or any language really).

If 10 is really passed to that function, that is certainly a bug. While there are better ways of doing it, that function should at least verify that pos is within the bounds of line before attempting to use it as an index.

like image 27
Jonathon Reinhart Avatar answered Sep 19 '22 02:09

Jonathon Reinhart