Thu 05 Jan 2017 08:43:45 PM UTC, original submission:
Tab handling, original version:
case '\t':
line.pos += (line.pos / 8 + 1) * 8;
if (line.pos >= (int)sizeof(line.buf))
line.pos = sizeof(line.buf) - 1;
break;
A possible working revision using a new integer variable opos:
int opos;
case '\t':
opos = line.pos;
line.pos = ((line.pos / 8) + 1) * 8;
if (line.pos >= (int)sizeof(line.buf))
line.pos = (int)sizeof(line.buf) - 1;
for ( ; opos <= line.pos; opos++) {
memcpy(line.buf + opos, " ", 1);
}
break;
2 problems with original:
1) The calculation to convert tabs to spaces is wrong.
2) Skipping forward leaves \0 characters so the line is cut at the first tab.
Variable used when uninitialised:
int main(int argc, char **argv)
{
char buf[1024];
if ((realfd = open_nb(realcons)) < 0) {
fprintf(stderr, "bootlogd: %s: %s\n", buf, strerror(errno));
return 1;
}
buf is uninitialised at this point in main. realcons was buf in open_nb, so is probably what was intended to be printed.
Testing the 2 tab versions, this is running a modified bootlogd from xfce4-terminal with the console device hardcoded.
$ sudo ./tabbootlogd
$ pidof tabbootlogd
1033
$ sudo su
# printf "hello again\n" >/dev/console
# printf "space tab\tnow 2 tabs:\t\t3:\t\t\t<\n" >/dev/console
# kill 1033
# exit
exit
$ sudo ./notab-bootlogd
$ pidof notab-bootlogd
1080
$ sudo su
# printf "hello again\n" >/dev/console
# printf "space tab\tnow 2 tabs:\t\t3:\t\t\t<\n" >/dev/console
The correct output is printed on the console device TTY1 (CTRL ALT F1, then CTRL ALT F7 to return) but the log file says this:
Thu Jan 5 14:00:31 2017: hello again
Thu Jan 5 14:01:42 2017: space tab now 2 tabs: 3: <
Thu Jan 5 14:11:37 2017: hello again
Thu Jan 5 14:11:51 2017: space tab
Correcting the line truncation issue but using the original calculation gives some sort of exponential tab size growth:
# printf "Original sum now\n" >/dev/console
# printf "space tab\tnow 2 tabs:\t\t3:\t\t\t<\n" >/dev/console
# printf "big tabs there\n" >/dev/console
Thu Jan 5 20:05:46 2017: Original sum now
Thu Jan 5 20:06:01 2017: space tab now 2 tabs:
3:
Thu Jan 5 20:06:32 2017: big tabs there
Also on the mailing list I saw you might include the Debian patches, one leaves a few unused functions and variables after it is applied, I didn't look at those patches in great detail though.
(Not sure if all the whitespace copied and pasted exactly but hopefully you will get the idea)
Best wishes.
|