Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tb_vsnprintf当第二个参数即缓冲区大小为0时,行为与标准库中的vsnprintf不一致 #276

Open
duyanning opened this issue Apr 2, 2025 · 3 comments

Comments

@duyanning
Copy link
Contributor

描述问题

tbox目前的实现是这样的

tb_long_t tb_vsnprintf(tb_char_t* s, tb_size_t n, tb_char_t const* fmt, tb_va_list_t args)
{
    // check
    if (!n || !s || !fmt) return 0;

即当n等于0时,直接返回0。

期待的结果

然而标准库里的vsnprintf的行为是这样的:
If bufsz is zero, nothing is written and buffer may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.

参考:https://en.cppreference.com/w/c/io/vfprintf

错误信息

如果可能,请尽量附加程序运行过程中的错误输出信息。

相关环境

请提供编译和运行环境信息。

其他信息

请提供其他附加信息帮助我们诊断问题。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: tb_vsnprintf When the second parameter, i.e., buffer size is 0, the behavior is inconsistent with vsnprintf in the standard library

@waruqi
Copy link
Member

waruqi commented Apr 2, 2025

这边不完全对标标准库,仅仅提供自有接口的 跨平台行为一致性,以及确保目前自己的需求够用。所以当初对于这种 极端不常用的 case ,完全没做处理,来尽可能轻量化。。没必要为了一堆 平常基本不会用到的 case 去写大量的处理代码。

正常情况下,这里,我都是可以直接 assert(n),强制报错,不让去传 n = 0 的 case。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


This is not completely benchmarking the standard library, but only provides cross-platform behavior consistency of its own interfaces and ensures that its current needs are sufficient. So at the beginning, this extremely infrequently used case was completely dealt with, and it was as light as possible. . There is no need to write a lot of processing code for a bunch of cases that you don't usually use.

Under normal circumstances, here, I can directly assert(n) and force an error to not be passed on a case with n = 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants