0

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

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;


}
15
  • 1
    I think instead of *contentKeyCtx = localckc; you meant contentKeyCtx = &localckc;. Commented Dec 7, 2018 at 14:35
  • The operator * can only be used when you have Uint8 ** contentKeyCtx initialized... Commented Dec 7, 2018 at 14:41
  • 1
    To understand your high-level issue you show to few code. So any answer saying more that you may not de-reference an uninitialised pointer (as you seem to be doing here: *contentKeyCtx = ...) would be just guessing. Commented Dec 7, 2018 at 14:51
  • 1
    As soon as you added main, John answered before I could have. Of course, he got it right. This entire comment conversation would have been unnecessary had you posted an MCV example. I'm not wanting you to interpret this as being mean-spirited. On the contrary, I want to be a good helper and encourage you to be more diligent in posting good questions, those that are more likely to get upvoted. Have a blessed day. Commented Dec 7, 2018 at 15:23
  • 1
    @jeff6times7 of course you are right, i saw what you mean when i saw the answer. Will have this in mind for future questions, Thanks Commented Dec 7, 2018 at 15:26

3 Answers 3

5

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

contentKeyCtx

Is not initialized, it's pointing to nothing.

5 Comments

How can i initialize it so the code can work properly?
You want to say contentKeyCtx were not initialised. **contentKeyCtx is something different.
UInt8 **contentKeyCtx = calloc(1, sizeof(Uint8*));
Yes, I made a mistake, I wanted to write contentKeyCtx but i wrote it with ** instead.
Although allocating space would resolve the immediate problem, it is probably the wrong solution.
1

What i am doing wrong?

There are two ways you can solve your problem:

  1. Initialize 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 (a.k.a &):

    contentKeyCtx = &localckc;
    

1 Comment

I still get Segmentation Fault. I think i need to allocate *contentKeyCtx in order to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.