c99 - Nongreedy fscanf and buffer overflow check in c -


i'm looking have fscanf identify when potential overflow happens, , can't wrap head around how best it.

for example, file containing string

**a**bb**cccc** 

i

char str[10]; while (fscanf(inputf, "*%10[^*]*", str) != eof) {  } 

because i'm guaranteed between ** , ** less 10. might a

**a**bb**cccc* 

(without last *) or potentially buffer overflow.

i considered using

while (fscanf(inputf, "*%10[^*]", str) != eof) {  } 

(without last *) or even

while (fscanf(inputf, "*%10s*", str) != eof) {  } 

but return entire string. tried seeing if check presence or lack of *, can't work. i've seen implementation of fgets, i'd rather not make complicated. ideas?

i'm not clear on want. skip on number of stars, , read 9 non-star characters buffer? if so, try this:

void read_field(file *fin, char buf[10]) {     int c;     char *ptr = buf;     while ((c = getc(fin)) == '*')         /*continue*/;     while (c != '*' && c != eof && ptr < buf+9)     {         *ptr++ = c;         c = getc(fin);     }     *ptr = '\0';     /* skip next star here? */ } 

you note not using fscanf. because fscanf more trouble it's worth. above more typing, can confident described doing.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -