简体   繁体   English

双指针分配中的分段错误

[英]Segmentation Fault in double pointer assignment

I have declared double pointer like this 我已经这样声明了双指针

UInt8  **contentKeyCtx;

This variable is sent as argument to a function Inside the function there is local variable 该变量作为参数发送给函数,函数内部有局部变量

 UInt8    *localckc     = NULL;
 localckc = calloc(1, localckcSize);

Then there is some array of values assigned to localckc 然后有一些值数组分配给localckc

When i try to do this 当我尝试这样做时

 *contentKeyCtx = localckc;

I got Segmentation Fault 我遇到了细分错误

What i am doing wrong? 我做错了什么? Main function 主功能

int main (int argc, char *argv[])
{
    OSStatus result; // SInt32


    UInt8  *inBuff, *outBuff;
    UInt32 inBuffSize, outBuffSize;
    UInt8  **contentKeyCtx = calloc(1, sizeof(UInt8**));
    UInt32  *contentKeyCtxSize;
    FILE   *fp;


    const UInt8 *assetId = {0x1b, 0xf7, 0xf5, 0x3f, 0x5d, 0x5d, 0x5a, 0x1f};// what is this?



    inBuff=calloc(1,INBUFFSIZE);
    outBuff=calloc(1,OUTBUFFSIZE);


    inBuffSize = fread(inBuff, sizeof(UInt8), INBUFFSIZE, fp);

    fclose(fp);


    // IK we have some data. Now what?
    result = SKDServerGenCKC(inBuff, inBuffSize, assetId, contentKeyCtx, contentKeyCtxSize);


    free(inBuff);
    free(outBuff);
    printf("Result is  %d\n", result );
    return 0;
}

Actual function implementation 实际功能实现

    OSStatus SKDServerGenCKC(
        const UInt8   *serverPlaybackCtx,
        UInt32         serverPlaybackCtxSize,
        const UInt8   *assetId,
        UInt8        **contentKeyCtx,
        UInt32        *contentKeyCtxSize)
{

    UInt8    *localckc     = NULL;
    UInt32    localckcSize = 0;
    PS_RequireAction(ckcContainer      != NULL, return kDRMSKDServerParamErr;)
    PS_RequireAction(contentKeyCtx     != NULL, return kDRMSKDServerParamErr;)
    PS_RequireAction(contentKeyCtxSize != NULL, return kDRMSKDServerParamErr;) 
    ...
     localckc = calloc(1, localckcSize);
     status = SKDServerWriteBytes(
                &ckcContainer->parser.currentOffset, PS_AES128_IV_SZ, 
                ckcContainer->aesKeyIV, localckcSize, localckc);


    ...
    *contentKeyCtx = localckc;


}

The function implementation you have now presented differs in an important way from your original description of the problem. 您现在介绍的功能实现与问题的原始描述在重要方面有所不同。 It is now clear that the pointer involved, contentKeyCtx , is a function parameter, not a local or file-scope variable, and the code shows that it is being used to convey a pointer computed by the function back to the function's caller. 现在很明显,所涉及的指针contentKeyCtx是一个函数参数,而不是局部变量或文件范围变量,并且代码显示该指针正用于将函数计算出的指针传递回函数调用者。

In this case, the corresponding actual argument to the function call should be the address of an appropriately-typed variable in which the function will store the generated pointer value, like so: 在这种情况下,函数调用的相应实际参数应为适当类型变量的地址,函数将在其中存储生成的指针值,如下所示:

UInt8        *contentKeyCtx;
UInt32       contentKeyCtxSize = 0;
OSStatus     status;

status = SKDServerGenCKC(..., &contentKeyCtx, &contentKeyCtxSize);

Similar applies to the last argument, as shown; 如图所示,最后一个参数也是如此。 it apparently is used to return the size of the space to which the returned pointer points. 它显然用于返回所返回指针所指向的空间的大小。

contentKeyCtx

没有初始化,它什么也没有指向。

What i am doing wrong? 我做错了什么?

There are two ways you can solve your problem: 有两种方法可以解决问题:

  1. Initialize contentKeyCtx : 初始化contentKeyCtx

     // Use 'calloc()', if you want it to be zero by default... UInt8 **contentKeyCtx = malloc(1, sizeof(Uint8*)); // Do something with it... 

    Or: Re-allocate it: 或者:重新分配它:

     UInt8 **contentKeyCtx; contentKeyCtx = realloc(NULL, sizeof(Uint8*)); // Do something with it... 
  2. Use the address-of operator (aka & ): 使用地址运算符(又名& ):

     contentKeyCtx = &localckc; 

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

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