Explorar el Código

Improve handling of arguments of the macros

The arguments are surrounded by parentheses to be evaluated first.
Stéphane Raimbault hace 14 años
padre
commit
39ccdf3234
Se han modificado 1 ficheros con 6 adiciones y 6 borrados
  1. 6 6
      src/modbus.h

+ 6 - 6
src/modbus.h

@@ -187,14 +187,14 @@ int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
  * UTILS FUNCTIONS
  * UTILS FUNCTIONS
  **/
  **/
 
 
-#define MODBUS_GET_HIGH_BYTE(data) ((data >> 8) & 0xFF)
-#define MODBUS_GET_LOW_BYTE(data) (data & 0xFF)
-#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[index] << 16) + tab_int16[index + 1])
-#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[index] << 8) + tab_int8[index + 1])
+#define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
+#define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
+#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])
+#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1])
 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
     do { \
     do { \
-       tab_int8[index] = value >> 8; \
-       tab_int8[index + 1] = value & 0xFF; \
+        tab_int8[(index)] = (value) >> 8;  \
+        tab_int8[(index) + 1] = (value) & 0xFF; \
     } while (0)
     } while (0)
 
 
 void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
 void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);