This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf_sdk: app_util.h CEIL_DIV implementation

app_util.h:106

#define CEIL_DIV(A, B)      \
     /*lint -save -e573 */   \
     ((((A) - 1) / (B)) + 1) \
     /*lint -restore */

Causes a compiler error (by sheer luck) when the numerator is 0. This can be seen in softdevice_handler.h:74

static uint32_t BLE_EVT_BUFFER[CEIL_DIV(BLE_STACK_EVT_MSG_BUF_SIZE, sizeof(uint32_t))];

when BLE_STACK_SUPPORT_REQD is not set (causing BLE_STACK_EVT_MSG_BUF_SIZE to be zero).

Suggested fix: Standard ceil macro implementation:

#define CEIL_DIV(A, B) (((A) + (B) - 1) / (B))
Parents
  • Thank you. I ran into this exact error, caused by the same thing.

    The CEIL_DIV macro is incorrect for the case of dividing 0 by any unsigned value.

    In C, division by an unsigned integer converts the other operand to unsigned, as demonstrated by this test program:

    #include <stdio.h>
    
    int main(void) {
    
      printf("%d\n",-1/(4u));
    
    }
    

    It is due to line 714 of the C standard c0x.coding-guidelines.com/6.3.1.8.html.

    If one of the operators of "/", then the other operator is cast to unsigned before the division occurs.

    sizeof() is an unsigned expression.

    Therefore the CEIL_DIV macro evaluates to a large number on a standards-compliant compiler.

Reply
  • Thank you. I ran into this exact error, caused by the same thing.

    The CEIL_DIV macro is incorrect for the case of dividing 0 by any unsigned value.

    In C, division by an unsigned integer converts the other operand to unsigned, as demonstrated by this test program:

    #include <stdio.h>
    
    int main(void) {
    
      printf("%d\n",-1/(4u));
    
    }
    

    It is due to line 714 of the C standard c0x.coding-guidelines.com/6.3.1.8.html.

    If one of the operators of "/", then the other operator is cast to unsigned before the division occurs.

    sizeof() is an unsigned expression.

    Therefore the CEIL_DIV macro evaluates to a large number on a standards-compliant compiler.

Children
Related