Kumar Roshan Mehta
Kumar Roshan Mehta

Reputation: 3306

Unable to understand casting in C

I have a library function whose signature is as follows:

sgx_status_t sgx_unseal_data(
const sgx_sealed_data_t * p_sealed_data,
uint8_t * p_additional_MACtext,
uint32_t * p_additional_MACtext_length,
uint8_t * p_decrypted_text,
uint32_t * p_decrypted_text_length
);

Now, for the uint32_t * p_decrypted_text_length.

Case 1: Pass

  char resecret[50];
  uint32_t resecret_len = sizeof(resecret);

  status = sgx_unseal_data(
          (sgx_sealed_data_t*)sealed_data,
          NULL,
          NULL,
          (uint8_t*)&resecret,
          (uint32_t*)&resecret_len); //Notice last parameter

Case 2: Fails

  char resecret[50];
//  uint32_t resecret_len = sizeof(resecret);
  status = sgx_unseal_data(
          (sgx_sealed_data_t*)sealed_data,
          NULL,
          NULL,
          (uint8_t*)&resecret,
          (uint32_t*)&(sizeof(resecret))); //Notice last parameter

My question is why the second case fails. What am I missing in casting? Also, if you could explain a little more about casting between uint_8t, uint_32t, size_t and their pointer cousins.

Upvotes: 0

Views: 90

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

What's probably happening is that the sgx_unseal_data function emulates pass by reference here.

You pass a pointer to a variable, so the function can dereference the pointer to do assignment.

The value returned by sizeof is a compile-time value, a so called rvalue. First of all you can't get the address of those values, it's like trying to get the address of an integer literal (like e.g. &12 which is nonsense). Secondly, even if it was valid to get the address of an rvalue, the assignment to that location would be worthless since you have no variable where it would be stored.

The problem have nothing to do with the casting at all, just that you can't get the address of an rvalue.

Upvotes: 3

Related Questions