gpt4 book ai didi

c++ - 如何从snprintf()获得-1

转载 作者:行者123 更新时间:2023-12-03 07:12:05 29 4
gpt4 key购买 nike

我正在做单元测试。我必须测试所有可能的if..else情况。但是在此if语句中:

int32_t i32Res = snprintf(buffer, len, "The%d_String_%d", 0, u8Idx);
if ((i32Res < 0) || (i32Res >= len))
{
return enuErrVal;
}
i32Res绝不是 < 0,它总是以字符串的大小值结尾。我只能强制 bufferlen变量。我尝试使用空 buffer,但是在实际到达 if之前会崩溃。我尝试使用 buffer中的非常小和非常大的大小。我尝试对 2使用低( len)值。
我知道 snprintf()会在编码错误时返回 -1,如何强制执行此操作?

最佳答案

https://support.sas.com/documentation/onlinedoc/sasc/doc700/html/lr1/z2056522.htm#z2056525:

The snprintf function returns an integer value that equals, inmagnitude, the number of characters written to the area addressed bydest. If the value returned is negative, then either the maxlencharacter limit was reached or some other error, such as an invalidformat specification, has occurred. The one exception to this is ifan error occurs before any characters are stored, snprintf returnsINT_MIN (-2**31) .

关于c++ - 如何从snprintf()获得-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64556890/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com