简体   繁体   English

Microsoft 的 SAL Deref=1 与“int”参数相关的属性?

[英]Microsoft's SAL Deref=1 property in relation to an “int” parameter?

I've come round to the idea that Microsoft's SAL (Source Annotation Language) is a good thing, and have studied the language and the meaning of annotation properties .想到微软的SAL(Source Annotation Language)是个好东西,研究了语言和注解属性的含义。

I have a general question about the use of SAL's " Deref " property in connection with an " int " parameter.我有一个关于将 SAL 的“ Deref ”属性与“ int ”参数结合使用的一般性问题。 Let me illustrate my question with the SAL for the isalpha() function, taken from the ctype.h include file, running Visual Studio 10:让我用 isalpha() function 的 SAL 来说明我的问题,取自 ctype.h 包含文件,运行 Visual Studio 10:

[returnvalue:SA_Post(MustCheck=SA_Yes)] int __cdecl isalpha([SA_Pre(Null=SA_No)] [SA_Pre(Deref=1,Valid=SA_Yes,Access=SA_Read)] int _C); [返回值:SA_Post(MustCheck=SA_Yes)] int __cdecl isalpha([SA_Pre(Null=SA_No)] [SA_Pre(Deref=1,Valid=SA_Yes,Access=SA_Read)] int _C);

If the single parameter _C is an " int ", what does "[SA_Pre( Deref=1 ,Valid=SA_Yes,Access=SA_Read)]" mean?如果单个参数 _C 是“ int ”,那么“[SA_Pre( Deref=1 ,Valid=SA_Yes,Access=SA_Read)]”是什么意思? How can one dereference an int once (Deref=1) in a meaningful way?如何以有意义的方式取消引用一次 int (Deref=1)?

The only explanation I can think of is that the annotation states that the integer is a reference into ctype's internal byte array.我能想到的唯一解释是注释指出 integer 是对 ctype 内部字节数组的引用。 How could a static analyzer take advantage of this annotation? static 分析仪如何利用此注释?

What it looks like is that you've pasted in the pre-processed version of the isalpha declaration.看起来您已经粘贴了 isalpha 声明的预处理版本。 What I see in ctype.h is:我在 ctype.h 中看到的是:

_Check_return_ _CRT_JIT_INTRINSIC _CRTIMP int __cdecl isalpha(_In_ int _C);

_In_ is allowed on scalar parameters (int, etc.) in order to let developers explicitly express that the parameter is strictly an input parameter. _In_允许在标量参数(int 等)上使用,以便让开发人员明确表示该参数是严格的输入参数。 This is kind of redundant, but still true (after all, you can't return a value via a pass-by-value scalar).这有点多余,但仍然是正确的(毕竟,您不能通过按值传递的标量返回值)。

The annotation _In_ is a macro that expands as you've pasted above in order to express the semantics of an input pointer .注释_In_是一个宏,它在您在上面粘贴时进行扩展,以表达输入指针的语义。 The static analyzer recognizes when _In_ is being applied to a scalar parameter and ignores it, since neither the Null nor the Deref=1 makes much sense on an int. static 分析器识别何时_In_应用于标量参数并忽略它,因为 Null 和 Deref=1 在 int 上都没有多大意义。

In any other context, besides being part of an _In_ annotation, Deref=1 on an int would make no sense.在任何其他情况下,除了作为_In_注释的一部分之外, int 上的 Deref=1 没有任何意义。

It's generally better to be using the _In_ -style syntax rather than the SA_Pre and SA_Post, unless you really want to be looking into the underlying implementation details like this.使用_In_风格的语法通常比使用 SA_Pre 和 SA_Post 更好,除非你真的想像这样研究底层的实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM