Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

Fix klibc function signatures to match standard libc conventions

Problem Summary

The issue occurs when using mlibc on qemu/aarch64. The klibc functions have non-standard type signatures that conflict with standard libc headers, causing compilation errors when libfdt_env.h defines #define strnlen rt_strnlen.

Solution

Updated all klibc function signatures to use standard C types to ensure compatibility with standard libc.

Implementation Plan

  • Analyze the issue and understand type conflicts
  • Update kstring.h header file with standard-compliant signatures
  • Update kstring.c implementation to use standard types
  • Update internal variable types in implementation
  • Update kstdio.h header file with standard-compliant signatures
  • Update kstdio.c implementation to use standard types
  • Update rt_vsnprintf_std.c implementation
  • Update rt_vsnprintf_tiny.c implementation
  • Suppress cppcheck preprocessorErrorDirective for FLT_RADIX check
  • Verify type compatibility with tests
  • Run security checks

Changes Made

include/klibc/kstring.h

All function signatures updated to use standard types:

  • void *rt_memset(void *s, int c, size_t n)
  • void *rt_memcpy(void *dest, const void *src, size_t n)
  • void *rt_memmove(void *dest, const void *src, size_t n)
  • int rt_memcmp(const void *cs, const void *ct, size_t count)
  • size_t rt_strnlen(const char *s, size_t maxlen)
  • int rt_strcasecmp(const char *a, const char *b)
  • int rt_strncmp(const char *cs, const char *ct, size_t count)
  • int rt_strcmp(const char *cs, const char *ct)
  • size_t rt_strlen(const char *src)
  • char *rt_strncpy(char *dest, const char *src, size_t n)

src/klibc/kstring.c

  • Updated all function implementations to match new signatures
  • Changed internal variables to use standard types

include/klibc/kstdio.h

All function signatures updated to use standard types:

  • int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  • int rt_snprintf(char *buf, size_t size, const char *format, ...)

src/klibc/kstdio.c

  • Updated function signatures: rt_size_tsize_t
  • Updated local variables: rt_int32_t nint n
  • Updated casts: (rt_size_t) - 1(size_t) - 1

src/klibc/rt_vsnprintf_std.c

  • Updated rt_vsnprintf function signature: rt_size_t sizesize_t size
  • Added cppcheck suppression for preprocessorErrorDirective on FLT_RADIX check

src/klibc/rt_vsnprintf_tiny.c

  • Updated rt_vsnprintf function signature: rt_size_t sizesize_t size
  • Updated va_arg for %z format: va_arg(args, rt_size_t)va_arg(args, size_t)

Testing

  • Verified type compatibility with synthetic test cases for both kstring and kstdio
  • Confirmed no security issues with CodeQL
  • No breaking changes to existing code
  • Added cppcheck suppression to prevent false positive on FLT_RADIX compile-time check
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] rtj_strnlen/strnlen中的混用问题。进而klibc参数和返回值混用的问题</issue_title>
<issue_description>### RT-Thread Version

master

Hardware Type/Architectures

qemu/aarch64

Develop Toolchain

GCC

Describe the bug

在qemu aarch64中使用mlibc后出现rt_strnlen/strnlen混用的问题:

In file included from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/ofw/libfdt/libfdt.h:9,
                 from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/include/drivers/ofw.h:19,
                 from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/include/drivers/ofw_io.h:15,
                 from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/core/platform_ofw.c:17:
/home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/ofw/libfdt/libfdt_env.h:27:17: error: conflicting types for 'rt_strnlen'; have 'size_t(const char *, size_t)' {aka 'long unsigned int(const char *, long unsigned int)'}
   27 | #define strnlen rt_strnlen
      |                 ^~~~~~~~~~
/home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/libc/compilers/common/include/posix/string.h:31:8: note: in expansion of macro 'strnlen'
   31 | size_t strnlen(const char *s, size_t maxlen);
      |        ^~~~~~~
In file included from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/include/rtklibc.h:17,
                 from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/include/rtthread.h:36,
                 from /home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/components/drivers/core/platform_ofw.c:11:
/home/runner/work/rtthread-qemu/rtthread-qemu/rt-thread/include/klibc/kstring.h:26:11: note: previous declaration of 'rt_strnlen' with type 'rt_size_t(const char *, rt_ubase_t)' {aka 'long unsigned int(const char *, long long unsigned int)'}
   26 | rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen);
      |           ^~~~~~~~~~
CC build/kernel/components/drivers/core/power_domain.o
scons: *** [build/kernel/components/drivers/core/platform_ofw.o] Error 1

原因应该是 rt_strnlen 并不是完全标准的strnlen声明方式(参数和返回值),需要rt_strnlen以完全的strnlen方式进行实现:
size_t strnlen(const char *s, size_t maxlen);

同样的,

rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count);
rt_int32_t rt_strcasecmp(const char *a, const char *b);
rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count);
rt_int32_t rt_strcmp(const char *cs, const char *ct);

中的rt_int32_t,也不应该采用这样的方式,而是int。

rt_memset 应该是 void *rt_memset(void *, int, size_t); 方式。

可以考虑 klibc 这部分的实现,各API保留 rt_ 前缀,但参数和返回值和标准的libc都一致的模式。

Other additional context

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix rtj_strnlen and strnlen mix issue [klibc] Align function signatures with standard libc to fix type conflicts Nov 4, 2025
Copilot AI requested a review from BernardXiong November 4, 2025 01:35
Copilot finished work on behalf of BernardXiong November 4, 2025 01:35
@github-actions github-actions bot added the Kernel PR has src relate code label Nov 4, 2025
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

📌 Code Review Assignment

🏷️ Tag: kernel

Reviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837

Changed Files (Click to expand)
  • src/klibc/kstdio.c
  • src/klibc/kstring.c
  • src/klibc/rt_vsnprintf_std.c
  • src/klibc/rt_vsnprintf_tiny.c

📊 Current Review Status (Last Updated: 2025-11-05 23:42 CST)

  • GorrayLi Pending Review
  • ReviewSun Pending Review
  • hamburger-os Pending Review
  • lianux-mm Pending Review
  • wdfk-prog Pending Review
  • xu18838022837 Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@BernardXiong
Copy link
Member

@copilot

针对rt_vsnprintf也一样进行处理;

以及包括 kstdio 中的 API。

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot

针对rt_vsnprintf也一样进行处理;
...

Updated kstdio functions to use standard types. Changes in commit 9652a0a:

kstdio.h:

  • rt_vsnprintf, rt_snprintf: Changed rt_size_t sizesize_t size

kstdio.c:

  • Updated function signatures to match header
  • Changed local variables: rt_int32_t nint n
  • Updated cast: (rt_size_t) - 1(size_t) - 1

All kstdio APIs now match standard libc signatures while retaining rt_ prefix.

Copilot finished work on behalf of BernardXiong November 4, 2025 01:46
@BernardXiong
Copy link
Member

@copilot 对于 include/klibc/kstdio.h 中的函数参数,也按照标准的方式进行更改,例如:

int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args);
int rt_snprintf(char *buf, rt_size_t size, const char *format, ...);

也完成对应实现的更改。

Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot 对于 include/klibc/kstdio.h 中的函数参数,也按照标准的方式进行更改,例如:

...

These changes were already completed in commit 9652a0a. Both rt_vsnprintf and rt_snprintf in kstdio.h now use size_t size (instead of rt_size_t size), and the corresponding implementations in kstdio.c have been updated accordingly.

Copilot finished work on behalf of BernardXiong November 4, 2025 03:40
@BernardXiong BernardXiong marked this pull request as ready for review November 4, 2025 05:32
@BernardXiong
Copy link
Member

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot

Updated both files in commit c351ee5:

rt_vsnprintf_std.c:

  • Changed function parameter: rt_size_t sizesize_t size

rt_vsnprintf_tiny.c:

  • Changed function parameter: rt_size_t sizesize_t size
  • Changed va_arg for %z format: va_arg(args, rt_size_t)va_arg(args, size_t)

All rt_vsnprintf implementations now use standard size_t type.

Copilot finished work on behalf of BernardXiong November 5, 2025 14:38
@BernardXiong
Copy link
Member

@copilot

[cpp_check.py 80 INFO] src/klibc/rt_vsnprintf_std.c:225:2: error: #error "Non-binary-radix floating-point types are unsupported." [preprocessorErrorDirective]
#error "Non-binary-radix floating-point types are unsupported."
 ^

针对以上ci时的cpp check,需要做对应的处理,尝试修复下。

Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot

...

Fixed in commit 774d9c1. Added // cppcheck-suppress preprocessorErrorDirective comment before the #error directive on line 225. This suppresses the cppcheck warning while preserving the compile-time check that ensures floating-point types use binary radix.

Copilot finished work on behalf of BernardXiong November 5, 2025 14:59
@Rbb666 Rbb666 merged commit d39a439 into master Nov 7, 2025
56 of 57 checks passed
@Rbb666 Rbb666 deleted the copilot/fix-rtj-strnlen-issue branch November 7, 2025 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kernel PR has src relate code +1 Agree +1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] rt_strnlen/strnlen中的混用问题。进而klibc参数和返回值混用的问题

4 participants