简体   繁体   English

指向呼叫者SAL错误的指针的输出

[英]Output of pointer to caller SAL error

I am trying to add SAL to my code... i worked according msdn and found bug in msdn examples, don't know how to deal with it. 我正在尝试将SAL添加到我的代码中...我根据msdn工作,并在msdn示例中发现了错误,不知道如何处理。

Here litle changed example "Output of pointer to caller (Example: The Outptr Annotation)" from Understanding SAL 在这里,litle从理解SAL更改了示例“指向调用者的指针输出(示例:Outptr注释)”

Outptr is used to annotate a parameter that's intended to return a pointer. Outptr用于注释旨在返回指针的参数。 The parameter itself should not be NULL, and the called function returns a non-NULL pointer in it and that pointer points to initialized data. 参数本身不应为NULL,被调用函数在其中返回一个非NULL指针,并且该指针指向已初始化的数据。

My code: 我的代码:

#include "stdafx.h"
#include "assert.h"

void GoodOutPtrCallee(_Outptr_ int **pInt)
{
    int *pInt2 = new int;

    if (*pInt != NULL)
    {
        *pInt2 = 1;
    }
    else
    {
        *pInt2 = 2;
    }

    *pInt = pInt2;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int* nullValue = NULL;
    GoodOutPtrCallee(&nullValue); 
    assert(*nullValue == 2);

    int someValue = 22;
    int* someValuePtr = &someValue;
    GoodOutPtrCallee(&someValuePtr); 
    assert(*someValuePtr == 1);

    return 0;
}

If i compile it in VS2013 with code alalysys enabled i got C6001: using uninitialized memory 如果我在启用了代码alalysys的VS2013中进行编译,则会得到C6001:使用未初始化的内存

for 对于

if (*pInt != NULL)

row. 行。

What is worng here in my annotation and how can i fix it? 我的注释中的磨损是什么,我该如何解决?

Since you're reading from the value passed through the pointer parameter pInt you can't use _Outptr_ , as this describes a parameter that's only used as an output, not also as an input. 由于您正在读取通过指针参数pInt传递的值,因此无法使用_Outptr_ ,因为_Outptr_描述了一个仅用作输出而不是输入的参数。 Use _Inout_ instead. 请改用_Inout_

You might want to reconsider using SAL. 您可能需要重新考虑使用SAL。 It's very poorly documented, and as a result I can't say with any certainty that _Inout_ is actually the best annotation to use here. 它的文档记录很少,因此,我不能肯定地说_Inout_实际上是在此使用的最佳注释。 All I know for sure is that it's best match I could find based on Microsoft's vague descriptions, and it gets rid of the warning. 我可以肯定的是,根据Microsoft含糊不清的描述,这是我能找到的最佳匹配,它摆脱了警告。 Of course so would not using an annotation. 当然不会使用注释。

EDIT : I was confused by similar variable names, pInt and pInt2 . 编辑 :我对类似的变量名pIntpInt2感到困惑。 You're probably should mark pInt as input and output, not just as output, because you're reading it's value to check whether it is NULL 您可能应该将pInt标记为输入和输出,而不仅仅是输出,因为您正在读取它的值以检查它是否为NULL

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

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