1
5
Fork
You've already forked bsdutils
1

Fix bugs with the conversion from fgetln to getline #46

Merged
dcantrell merged 1 commit from dashea/bsdutils:fgetln into main 2025年10月29日 00:35:23 +01:00
Contributor
Copy link

The length set by fgetln is always the length of the line ending at the
newline character, while the length set by getline includes the null
byte and any extra bytes that may be allocated for the buffer. Change
the getline calls that were converted from fgetln to use the return
value of getline in place of the buffer length parameter.

Update the example code in DIFFERENCES to separate the line length and
buffer length variables.

Tested this against tag v13.2: xargs -p is only looking for y/n so this doesn't change much there other than fixing the uninitialized buffer, and I couldn't get cut to actually fail, but this definitely makes a difference with join and head:

for example, for join, running echo '1,one' > file-1 ; echo '1,uno' > file-2 ; join -t, file-1 file-2

before, results in:

1,one 
,uno

and after:

1,one,uno

for head, tested with echo line1 > test-file ; echo line2 >> test-file ; head -1 test-file | xxd

before:

00000000: 6c69 6e65 310a 0000 0000 0000 0000 0000 line1...........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000070: 0000 0000 0000 0000 ........

after:

00000000: 6c69 6e65 310a line1.
The length set by fgetln is always the length of the line ending at the newline character, while the length set by getline includes the null byte and any extra bytes that may be allocated for the buffer. Change the getline calls that were converted from fgetln to use the return value of getline in place of the buffer length parameter. Update the example code in DIFFERENCES to separate the line length and buffer length variables. Tested this against tag v13.2: `xargs -p` is only looking for y/n so this doesn't change much there other than fixing the uninitialized buffer, and I couldn't get `cut` to actually fail, but this definitely makes a difference with `join` and `head`: for example, for join, running `echo '1,one' > file-1 ; echo '1,uno' > file-2 ; join -t, file-1 file-2` before, results in: ``` 1,one ,uno ``` and after: ``` 1,one,uno ``` for head, tested with `echo line1 > test-file ; echo line2 >> test-file ; head -1 test-file | xxd` before: ``` 00000000: 6c69 6e65 310a 0000 0000 0000 0000 0000 line1........... 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000070: 0000 0000 0000 0000 ........ ``` after: ``` 00000000: 6c69 6e65 310a line1. ```
The length set by fgetln is always the length of the line ending at the
newline character, while the length set by getline includes the null
byte and any extra bytes that may be allocated for the buffer. Change
the getline calls that were converted from fgetln to use the return
value of getline in place of the buffer length parameter.
Update the example code in DIFFERENCES to separate the line length and
buffer length variables.

I mean sure, if you want it to be correct. Thanks though, I appreciate it.

I mean sure, if you want it to be correct. Thanks though, I appreciate it.
Sign in to join this conversation.
No reviewers
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dcantrell/bsdutils!46
Reference in a new issue
dcantrell/bsdutils
No description provided.
Delete branch "dashea/bsdutils:fgetln"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?