tbox icon indicating copy to clipboard operation
tbox copied to clipboard

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

Open duyanning opened this issue 11 months ago • 3 comments

描述问题

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

错误信息

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

相关环境

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

其他信息

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

duyanning avatar Apr 02 '25 03:04 duyanning

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

Issues-translate-bot avatar Apr 02 '25 03:04 Issues-translate-bot

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

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

waruqi avatar Apr 02 '25 06:04 waruqi

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.

Issues-translate-bot avatar Apr 02 '25 06:04 Issues-translate-bot