“return +1” 在 Windows API 缓冲区注释中是什么意思?

huangapple go评论100阅读模式
英文:

what does (return +1) mean in windows API buffer annotation?

问题

"return + 1" 在这里 的意思是什么?

英文:

What does return + 1 mean here

DWORD
WINAPI
GetModuleFileName(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize, return + 1) LPTSTR lpFilename,
    __in DWORD nSize
    );

答案1

得分: 4

它的意思是“比调用返回的值多一个”。 __out_ecount_part 注释希望其 length 表达式包括 NUL 终止符,而 GetModuleFileName 的返回值不包括终止符,所以要加 +1 来考虑这个差异。

如果函数成功,返回值是复制到缓冲区的字符串的长度(以字符计算),不包括终止 null 字符。

英文:

It means "one more than the value returned by the call". __out_ecount_part annotation wants its length expression to include the NUL terminator, and the GetModuleFileName return value doesn't include the terminator, so +1 to account for the difference.

> If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters, not including the terminating null character.

答案2

得分: 3

在"C"中,所有字符串都以'\0'结尾。因此,+1允许在文件名末尾添加额外的'\0'。

英文:

In "C" all strings are '\0' terminated. so the +1 allows for that extra '\0' at the end of the filename

答案3

得分: 1

SAL 注解通常是由原语组合而成的。具体而言,__out_ecount_part 表示函数通过该参数写入输出 (_out)。索引用于元素而不是字节 (_ecount),并且该函数仅写入缓冲区的一部分 (_part)。

注解中的最后两个参数表示缓冲区的总大小和初始化长度(返回时)。总大小很简单,因为该值有一个名称 (nSize)。然而,初始化长度是函数的返回值,没有名称。SAL 使用 return 来引用它。

因此,return + 1 表示在返回时,从开头到(但不包括)函数返回值之前的索引处,缓冲区已经填充。+ 1 是为了考虑到零终止符,这是 C 中最常见的字符串编码方式。

请注意,SAL 注解仅由静态代码分析工具评估。对于 C 或 C++ 编译器来说,这都没有意义。SAL 注解被实现为预处理宏,并在预处理过程中完全清除。这允许使用其他保留关键字(例如 return)。

英文:

SAL annotations are generally built as combinations of primitives. __out_ecount_part, specifically, denotes a parameter that the function writes output through (_out). Indices index into elements as opposed to bytes (_ecount), and the function only writes to part of the buffer (_part).

The final two arguments in the annotation denote the buffer's total size and initialized length (on return). The total size is simple, as that value has a name (nSize). The initialized length, however, is the return value of the function, which doesn't have a name. SAL uses return to refer to it.

return + 1 thus states that, on return, the buffer has been filled from the start up to (but not including) the index one past the function's return value. The + 1 accounts for the zero-terminator, the most common encoding for strings in C.

Note that SAL annotations are evaluated by static code analyzer tools only. None of this is meaningful to a C or C++ compiler. SAL annotations are implemented as preprocessor macros and get fully purged during pre-processing. This allows the use of otherwise reserved keywords (such as return).

huangapple
  • 本文由 发表于 2023年7月31日 23:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定