Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bug/error in app_util.h

Hi,

This is more of a bug report than a question, but not sure how to get in touch with engineers outside the forum.

In app_util.h (Using SDK 12.3.0) there are two functions for encoding a uint32_t to a byte array:

static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
{
    p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
    p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
    p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
    p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
    return sizeof(uint32_t);
}

static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
{
    *(uint32_t *)p_encoded_data = __REV(value);
    return sizeof(uint32_t);
}
The normal one works as expected, but the uint32_big_encode will only work with 4-byte aligned memory (on nRF51822) or it will generate a hard fault. The correct way to do it is the following code:
static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
{
    p_encoded_data[3] = (uint8_t) ((value & 0x000000FF) >> 0);
    p_encoded_data[2] = (uint8_t) ((value & 0x0000FF00) >> 8);
    p_encoded_data[1] = (uint8_t) ((value & 0x00FF0000) >> 16);
    p_encoded_data[0] = (uint8_t) ((value & 0xFF000000) >> 24);
    return sizeof(uint32_t);
}
/Jimmie
Parents
  • Hi,

    Thank you for reporting this issue. There are many functions in the SDK that require word aligned pointers, as is the case here. However, in this case the other encoding functions does not have a alignment requirement, so I agree that the SDK should either use the implementation you suggest so that it works without word alignment, or alt the very least the API documentation should state that p_encoded_data must be word aligned. I have created an internal bug report so that it can be improved in a future SDK version.

Reply
  • Hi,

    Thank you for reporting this issue. There are many functions in the SDK that require word aligned pointers, as is the case here. However, in this case the other encoding functions does not have a alignment requirement, so I agree that the SDK should either use the implementation you suggest so that it works without word alignment, or alt the very least the API documentation should state that p_encoded_data must be word aligned. I have created an internal bug report so that it can be improved in a future SDK version.

Children
No Data
Related