Code smell not appropriate for us - MISRA C:2004, 16.7

c
sonarcfamily

(coulet ) #1

In this example in the following function the SonarQube requests to apply rule “Pointer and reference parameters should be “const” if the corresponding object is not modified”

We believed that this is not good because the pointed buffer is changed inside the nrfx_spim_xfer().
Other problem is that if we make is const just to make SQ happy, the cmok doesn’t generate the appropriate mocking functions for reading values through pointer because it expects(correctly) the value pointed never changes (TDD testing)

/**
 * @brief Transfer up to 255 bytes through SPI
 *
 * @param[in]     device     The device with which communication is performed
 * @param[in]     nb_bytes   The number of bytes to transfer
 * @param[inout]  tx_buffer  The buffer containing the bytes to write
 * @param[inout]  rx_buffer  The buffer containing the bytes to read
 */
static void spi_nrf_transfer(const spi_device_cfg_t* const device,
                             uint8_t                       nb_bytes,
                             const uint8_t* const          tx_buffer,
                             uint8_t* const                rx_buffer)
// Mark "rx_buffer" as const at every possible pointer level.

{
    nrfx_spim_xfer_desc_t nrfx_xfer_desc = {.p_rx_buffer = (uint8_t*)rx_buffer,
                                            .rx_length   = nb_bytes,
                                            .p_tx_buffer = tx_buffer,
                                            .tx_length   = nb_bytes};

    nrfx_spim_xfer(&spi_nrf_nrfx_spim_instances[device->spi_driver->module], &nrfx_xfer_desc, 0);
}

Do you have a way to improve this rule to avoid a code smell in this specific case ?
Best regards,
Jean-Christophe Coulet


(Loïc Joly) #2

We have a ticket to improve this rule: https://jira.sonarsource.com/browse/CPP-2009

I added to this ticket a self-contained sample code that reproduces your situation:

typedef struct {
  char *data;
} Data;

void f(Data theData);

void zorg(char * const c)
{
  Data theData = { .data = c};
  f(theData);
}

(coulet ) #3

Many thanks but did you know when this ticket will be completed ?
best regards,
Jean-Christophe


(Loïc Joly) #4

I do not know.
You can watch this issue to be notified when it is corrected.