void lol(char *str) {
str[strlen(str)+1];
}
^B~^B~^B~^B~^B~^B~^B~^B~
^B~^B~^YM^YM^YM^YM^B~^B~
^B~^YM^YM^B~^B~^GF^B~^B~
^GF^GF^YM^GF^GF^GF^GF^B~
[1;34m~[1;34m~[1;34m~[1;34m~[1;34m~[1;34m~[1;34m~[1;34m~
[1;34m~[1;34m~[1;33mM[1;33mM[1;33mM[1;33mM[1;34m~[1;34m~
[1;34m~[1;33mM[1;33mM[1;34m~[1;34m~[1;32mF[1;34m~[1;34m~
[1;32mF[1;32mF[1;33mM[1;32mF[1;32mF[1;32mF[1;32mF[1;34m~
[/code]
It seems it compresses adjacent color codes like in the following..
[code]
^cHello ^cWorld ^gtoday^^s before^D ^Z^R^R^R^R tomorr^yow^
[22;36mHello [22;36mWorld [22;32mtoday^[22;37m before^D [1;31m tomorr[22;33mow^
[/code]
It doesn't handle the 'today^^s' above correctly. I was expecting ^^ to be an escape for ^ and generate no color code.
Also it keeps the invalid color code ^D in the string, but not the invalid color code ^Z.
Also if I overwrite the NUL character in..
[code]
^cHello ^cWorld ^gtoday^^s before^D ^Z^R^R^R^R tomorr^yow^
[/code]
with the letter Q it creates the following..
[code]
[22;36mHello [22;36mWorld [22;32mtoday^[22;37m before^D [1;31m tomorr[22;33mow^Q
[/code]
char *cc(char *s,size_t sz) {
char lc=0,ef=0,*rp=s,*wp=s,esc=0136,*c="abcegjlmoprstvwyABCEGJLMOPRSTVWY";
search:
if (!*rp || (rp == s+sz)) goto done;
if (ef) goto escaped;
if (*rp==esc) ef=1;
else *wp++=*rp;
rp++;
goto search;
escaped:
if (strchr(c,*rp)) { if (lc != *rp) {lc=*rp;*wp++=esc;*wp++=*rp; }}
else {*wp++=esc;*wp++=*rp;}
ef=0;
rp++;
goto search;
done:
*wp=0;
return s;
}
No, the example Vigud posted is perfectly valid and safe code.
Really, it is so simple to access whatever memory you want without ever being told no ? Somehow I doubted it.
I'm talking specifically about Vigud's example - the one he posted in a code block. You replied to him with the comment "your program may segfault". However that is not true, his example is perfectly safe.
No it is not fine as you go over pti[1] ^ could very well be the last char in your string. pt[1] could be '\0' pti[2] & pti[3] coudl be pretty much anything outside of your string.
I'm afraid Scandum is correct. The above block of code is a perfectly valid way of dealing with NUL-terminated strings.