You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "tmedicci (via GitHub)" <gi...@apache.org> on 2025/02/05 18:06:21 UTC
[PR] libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard [nuttx]
tmedicci opened a new pull request, #15772:
URL: https://github.com/apache/nuttx/pull/15772
## Summary
* libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard
According to https://pubs.opengroup.org/onlinepubs/9799919799/, the expected behavior for the `wcstombs()` function is that it should convert the wide-character codes stopping conversion either when a character sequence exceeds the limit of `n` total bytes or if a null byte is stored. In the first case, the null-terminated value should not be appended to the dst buffer.
Currently, no null-terminator is appended to the dst buffer, even when it's expected to be appended, generating erroneous output.
This can be checked with the application at https://github.com/apache/nuttx-apps/pull/2986. Please note that the same application can be built in the host system and the expected output checked against NuttX's current behavior.
## Impact
The current implementation of `wcstombs` impacts all architectures. If the `dst` buffer is zero-filled before the operation, it would not generate trouble while checking for the null-terminated string. However, this may not be true if the buffer contains random data. This PR fixes the erroneous implementation. Please note that it should be fixed at `wcstombs`, once the `wcsnrtombs` (called by the previous function) is not expected to append the null-terminator.
## Testing
This depends on the testing application at https://github.com/apache/nuttx-apps/pull/2986
### On NuttX:
Internal CI testing + `rv-virt:nsh`:
Build with:
```
make -j distclean && ./tools/configure.sh rv-virt:nsh && kconfig-tweak -e LIBC_LOCALE && kconfig-tweak -e TESTING_WCSTOMBS && make olddefconfig && make
```
Run with:
```
$ qemu-system-riscv32 -semihosting -M virt,aclint=on -cpu rv32 -smp 1 -bios none -kernel nuttx -nographic
ABC
NuttShell (NSH) NuttX-10.4.0
nsh> wcstombs
wcstombs Test application:
Test Scenario: len is bigger than the size of the converted string. Expected the null-terminator at the end of the converted string.
Return code: 13
dst buffer (as uint8_t array): 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 00
The character just after the return value is the null terminating character.
Test Scenario: len is exactly the size of the converted string. Do not expected the null-terminator at the end of the converted string.
Return code: 13
dst buffer (as uint8_t array): 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 aa
The character just after the return value is the expected 0xaa value. No null-terminator.
Test Scenario: len is smaller than the size of the converted string. Do not expected the null-terminator at the end of the converted string.
Return code: 12
dst buffer (as uint8_t array): 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 aa aa
The character just after the return value is the expected 0xaa value. No null-terminator.
nsh>
```
### On host system
The application at https://github.com/apache/nuttx-apps/pull/2986 can be built with:
```
gcc -o wcstombs_example ../apps/testing/libc/wcstombs/wcstombs_main.c
```
Its output should be the same on target and host.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard [nuttx]
Posted by "nuttxpr (via GitHub)" <gi...@apache.org>.
nuttxpr commented on PR #15772:
URL: https://github.com/apache/nuttx/pull/15772#issuecomment-2637690519
[**\[Experimental Bot, please feedback here\]**](https://github.com/search?q=repo%3Aapache%2Fnuttx+13552&type=issues)
This PR appears to mostly meet the NuttX requirements, but some clarifications and additions are needed.
**Strengths:**
* **Clear problem description:** The summary clearly explains the bug in `wcsrtombs()` and its deviation from the POSIX standard.
* **Good use of references:** Linking to the POSIX specification and the NuttX Apps PR is helpful.
* **Impact description is reasonable:** The explanation of the impact on users and architectures is adequate.
* **Testing is detailed:** The testing instructions and logs are thorough and demonstrate the fix.
**Areas for Improvement:**
* **Summary - What functional part?** Be more specific than "libc/wcsrtombs". Mention the affected function(s) directly, e.g., `wcsrtombs()`, `wcsnrtombs()`.
* **Impact - Be more explicit with YES/NO answers:** Instead of writing prose, explicitly state `YES` or `NO` for each impact category, then provide the description. This makes the PR easier to scan. Example:
* Impact on user: YES (Users relying on the correct POSIX behavior of `wcsrtombs()` will see a change. Previously, the function did not null-terminate the output string correctly in certain cases, potentially leading to errors. This fix ensures correct null-termination, aligning with the standard.)
* Impact on build: NO
* Impact on hardware: YES (All architectures are affected as this is a libc change.)
* Impact on documentation: YES (The documentation for `wcsrtombs()` should be updated to reflect the corrected behavior.) Even if you haven't updated the documentation yet, stating it *needs* updating is important. Ideally, include the documentation update in the same PR.
* Impact on security: Potentially YES. Incorrect string termination can lead to buffer overflows in certain scenarios. Analyze this more carefully and provide a definitive YES/NO and explanation.
* Impact on compatibility: YES (This fixes a non-compliant behavior. Applications relying on the previous, incorrect behavior might break, although that behavior was buggy to begin with.)
* **Testing - Host details:** The host testing details are insufficient. Specify the OS, CPU architecture, and compiler version used for host testing. Example: `Build Host: macOS Ventura (Intel), Apple clang version 14.0.0`.
* **Testing - Consider additional architectures:** Testing on a single architecture (rv-virt) is good, but testing on at least one more, especially a significantly different one (e.g., ARM), would strengthen the PR.
* **Testing - Before Change Logs:** Include the *incorrect* output from the test application before the fix. This clearly demonstrates the problem being addressed.
**Example of improved Impact Section:**
```
## Impact
* Impact on user: YES (Users relying on the correct POSIX behavior of `wcsrtombs()` will see a change. Previously, the function did not null-terminate the output string correctly in certain cases, leading to potential errors. This fix ensures correct null-termination, aligning with the standard.)
* Impact on build: NO
* Impact on hardware: YES (All architectures are affected, as this is a libc change.)
* Impact on documentation: YES (The documentation for `wcsrtombs()` should be updated to reflect the corrected behavior.)
* Impact on security: MAYBE (Incorrect string termination *can* lead to security vulnerabilities like buffer overflows, depending on how the function is used. Further analysis is needed to determine the exact impact. [Provide more detail about the analysis or state why it is difficult to assess.])
* Impact on compatibility: YES (This fixes a non-compliant behavior. Applications relying on the previous, incorrect behavior might break, although that previous behavior was buggy and non-standard.)
* Anything else to consider: None
```
By addressing these points, the PR will be more complete and easier for reviewers to assess. This improves the chances of a swift and successful merge.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] libc/wcsrtombs: Fix the wcsrtombs() according to the POSIX standard [nuttx]
Posted by "jerpelea (via GitHub)" <gi...@apache.org>.
jerpelea merged PR #15772:
URL: https://github.com/apache/nuttx/pull/15772
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org