mb2hal: fix core dump on version 1.0#2278
Conversation
Always check if pin memory is really allocated before using it. When running as version 1.0, some pins are not allocated.
|
This looks reasonable to me. |
|
No precaution. mb2hal flat out crashes soon after communication starts when trying to use in v1.0 compatibility mode. |
|
I think this has nothing to do with the compatibility mode or not. |
|
Yes, I could have added the checks only where strictly needed, but for consistency and maintainability I thought it easier to add them everywhere. |
|
Sorry for not being clear enough about this. Should I prepare a stricter version? |
|
Can you say in which case it crashes in your configuration? |
|
[Thread debugging using libthread_db enabled] |
You added these checks only for register-based modbus transactions. Wouldn't it be also good to have them also for coil-based ones? |
|
You're totally right. For some reason I just focussed on float_value/int_value. It is also necessary at least for bit_inv. I prepared a commit: 1f4ea53 I again added the check to both bit and bit_inv, although it isn't strictly necesasry for bit. Unfortunately I can't easily test it, because my spindle doesn't support that access. |
|
Thanks. |
|
Oh, the root cause is simple. The memory for the float/int/.../value arrays get allocated in mb2hal_hal.c, and zeroed. Those arrays contain the pointers to the hal pins and get set with hal_pin_xxx_newf. But on version 1.0, some of those allocs get skipped, because the pins are not created, leaving the array elements as NULL. But mb2hal_modbus.c used to write to/read from those pointers regardless of the version, thus dereferencing NULL. |
|
Yeah I see it I should have made the distinction of version also in I think |
|
Given the argument of self-explaning, I made a patch with version checks instead: 95e7235 (based on a revert of the previous fix). I think it is fine, too. You decide :) |
I like that version, thanks :) |
|
To be honest, I like this version better, too. It turned out much smaller than I expected. I'll prepare the PR tomorrow. |
Always check if pin memory is really allocated before using it. When running as version 1.0, some pins are not allocated.
This is one of many possible solutions. I decided against checking the version here and instead always check as I think it is a bit more robust against future changes.