Dill
Dill

Reputation: 2025

function with return type uint8_t returns 4 bytes

EDIT: the solution to the problem was a missing prototype.

When i call uint8_t foo(uint16_t bar) and assign its return value to a uint32_t temp not only the LSB of temp is overwritten but more. How is this even possible? Compiler is GCC.

The calling code:

uint32_t temp = 0xAAAAAAAA;
printf("[%x]\n", temp);
temp = foo(me->bar);
printf("[%x]\n", temp);

the output:

[aaaaaaaa]
[4081ff]

Here the relevant parts of the implementation of foo.

typedef struct CMOCK_foo_CALL_INSTANCE_tag
{
  UNITY_LINE_TYPE LineNumber;
  uint8_t ReturnVal;
  int CallOrder;
  uint16_t Expected_nGoNo;
} CMOCK_foo_CALL_INSTANCE;

uint8_t foo(uint16_t bar)
{
  CMOCK_foo_CALL_INSTANCE* cmock_call_instance =
    (CMOCK_foo_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.foo_CallInstance);
  (...)
  return cmock_call_instance->ReturnVal;
}

Upvotes: 3

Views: 3630

Answers (1)

twalberg
twalberg

Reputation: 62369

Make sure that the file that calls temp = foo(me -> bar); includes either the prototype or the full definition of the uint8_t foo(uint16_t bar); function before it is called, either via an #include directive, or by adding the prototype/definition earlier in the file.

Without an already-seen signature for the function, the compiler will assume the signature to be int foo(); and generate code accordingly.

Using gcc -W -Wall to compile should generate warnings when implicit function signature assumptions are made.

Upvotes: 4

Related Questions