Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only load each wide integer once in MODBUS_SET_INT*_TO_INT*() macros #684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Apr 2, 2023

  1. Only load each wide integer once in MODBUS_SET_INT*_TO_INT*() macros

    If you have a union like this as part of the read data:
    	union {
    		uint8_t serial[6];
    		uint16_t serial_raw[3];
    	};
    and do the obvious
    	for(size_t i = 0; i < sizeof(rdng.serial_raw) / sizeof(*rdng.serial_raw); ++i) {
    		 MODBUS_SET_INT16_TO_INT8(rdng.serial, i*2, rdng.serial_raw[i]);
    	}
    then because serial_raw[i] is updated through serial[i] at first, then
    loaded again, you end up with rdng.serial[i*2]=rdng.serial[i*2+1] for
    all i, which is both confusing and wrong; instead, you must do
    	for(size_t i = 0; i < sizeof(rdng.serial_raw) / sizeof(*rdng.serial_raw); ++i) {
    		 uint16_t r = rdng.serial_raw[i];
    		 MODBUS_SET_INT16_TO_INT8(rdng.serial, i*2, rdng.serial_raw[i]);
    	}
    but there's really no reason to require this,
    and this patch lets you use them directly
    nabijaczleweli committed Apr 2, 2023
    Configuration menu
    Copy the full SHA
    6ee7b3f View commit details
    Browse the repository at this point in the history