瀏覽代碼

Improve handling of arguments of the macros

The arguments are surrounded by parentheses to be evaluated first.
Stéphane Raimbault 14 年之前
父節點
當前提交
39ccdf3234
共有 1 個文件被更改,包括 6 次插入6 次删除
  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
  **/
 
-#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) \
     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)
 
 void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);