All checks were being done where the line_size check was done last. This
allows data to be read from one past teh end of the line buffer. In C,
accessing data outside of an array is undefined behavior and may cause
yet known problems. Additionally, the compiler may end up making some
unreasonable assumptions under the pretense that the programmer is never
wrong and would not access data outside of the array.
while (getline(&line, &line_size, fd) > 0) {
/* move to first ':' */
i = 0;
- while ((line[i] != ':') && (line[i] != '\0')
- && (i < line_size)) {
+ while ((i < line_size) && (line[i] != '\0')
+ && (line[i] != ':')) {
i++;
}
while (getline(&line, &line_size, fd) > 0) {
/* move to first ':' */
i = 0;
- while ((line[i] != ':') && (line[i] != '\0')
- && (i < line_size)) {
+ while ((i < line_size) && (line[i] != ':')
+ && (line[i] != '\0')) {
i++;
}
while (getline(&line, &line_size, fd) > 0) {
/* move to first ':' */
i = 0;
- while ((line[i] != ':') && (line[i] != '\0')
- && (i < line_size)) {
+ while ((i < line_size) && (line[i] != '\0')
+ && (line[i] != ':')) {
i++;
}