RT-Thread / RT-Thread/rt-thread

[Bug] rt_vsnprintf格式化long long数值错误

Open
#11,596 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

RT-Thread Version

all

Hardware Type/Architectures

all

Develop Toolchain

Other

Describe the bug

rt_vsnprintf_tiny.c 中rt_vsnprintf函数格式化long long数值错误,主要原因是print_number中将 'l' 与 'L'合并处理了

        switch (qualifier)
        {
        case 'h':
            if ((rt_int16_t)num < 0)
            {
                sign = '-';
                num = (rt_uint16_t)-num;
            }
            break;
        case 'L':
        case 'l':
            if ((long)num < 0)
            {
                sign = '-';
                num = (unsigned long)-num;
            }
            break;

使用以下代码测出结果不同

rt_kprintf("rt_kprintf: INT32_MIN = %lld\n", (int64_t)INT32_MIN);
printf("printf: INT32_MIN = %lld\n", (int64_t)INT32_MIN);
rt_kprintf("rt_kprintf: UINT32_MAX = %lld\n", (int64_t)UINT32_MAX);
printf("printf: UINT32_MAX = %lld\n", (int64_t)UINT32_MAX);
rt_kprintf("rt_kprintf: INT64_MIN = %lld\n", (int64_t)INT64_MIN);
printf("printf: INT64_MIN = %lld\n", (int64_t)INT64_MIN);
rt_kprintf("rt_kprintf: INT64_MAX = %lld\n", (int64_t)INT64_MAX);
printf("printf: INT64_MAX = %lld\n", (int64_t)INT64_MAX);

测试结果

rt_kprintf: INT32_MIN = -2147483648
printf: INT32_MIN = -2147483648
rt_kprintf: UINT32_MAX = -1
printf: UINT32_MAX = 4294967295
rt_kprintf: INT64_MIN = 9223372036854775808
printf: INT64_MIN = -9223372036854775808
rt_kprintf: INT64_MAX = -1
printf: INT64_MAX = 9223372036854775807
Other additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in rt_vsnprintf_tiny.c at rt_vsnprintf and print_number, focusing on how the 'l' and 'L' qualifiers handle signed and unsigned values. Run the provided INT32 and INT64 formatting cases and compare rt_kprintf with printf. Done means the reported long long values match the expected printf output.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot, operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.