fix: use fgets return value as loop condition in exec()#1211
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
de7afa8 to
3f5f5d3
Compare
|
/azpw run Azure.sonic-swss-common |
|
Retrying failed(or canceled) jobs... |
|
/azp run |
|
No Azure DevOps builds found for #1211. |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks! ---Powered by SONiC BuildBot
|
|
/azpw run Azure.sonic-swss-common |
|
Retrying failed(or canceled) jobs... |
|
Retrying failed(or canceled) stages in build 1144007: ✅Stage Test:
|
|
/azpw run Azure.sonic-swss-common |
|
Retrying failed(or canceled) jobs... |
|
Retrying failed(or canceled) stages in build 1144007: ✅Stage Test:
|
Using !feof() as a loop condition is a classic bug: feof() only becomes true after a read has already hit EOF, causing one extra iteration where fgets() returns NULL. It also does not handle I/O errors (ferror), which would cause an infinite loop. Fix: drive the loop directly on the fgets() return value, which handles both EOF and error correctly in a single condition. Signed-off-by: xq9mend <xq9mend@users.noreply.github.com>
3f5f5d3 to
2aac3f5
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
The The code change in this PR is correct and unrelated to p4rt. |
|
/azp run Azure.sonic-swss-common |
|
Commenter does not have sufficient privileges for PR 1211 in repo sonic-net/sonic-swss-common |
|
/azp run Azure.sonic-swss-common |
|
Commenter does not have sufficient privileges for PR 1211 in repo sonic-net/sonic-swss-common |
What
Fix incorrect loop condition in
exec()incommon/exec.cpp.Why
Using
!feof(pipe)as a loop condition has two problems:feof()only becomes true after a read has already hit EOF, so the loop always executes one extra iteration wherefgets()returns NULL.feof()does not detect I/O errors (ferror). On a read error,fgets()returns NULL butfeof()remains false, causing an infinite loop.How
Drive the loop directly on the
fgets()return value. This handles both EOF and I/O errors correctly with a single condition, and eliminates the redundant innerifguard.