Fix various problems and bugs detected by cppcheck#3334
Fix various problems and bugs detected by cppcheck#3334BsAtHome merged 17 commits intoLinuxCNC:masterfrom
Conversation
… no side-effects can occur. Fix missing dereference in clearing pin reference.
…will improperly clear the value entirely. The abs() function must be promoted in size and use labs().
Fix wrong variable in NULL test (copy/paste error).
Add LocalFree() to prevent leak.
|
This is very nice work of yours, thank you tons for this. |
smoe
left a comment
There was a problem hiding this comment.
My attempts to educate myself on this one have failed. My initial hunch was "alignment", but that should have happened right at the initialisiation of the char*? Maybe by introducing https://en.cppreference.com/w/cpp/language/alignas ?
The transition from the char* pByte to the double* could be more formally done by reinterpret_cast, but since the compiler already accepts this, I think this this just fine?
This is C-code, no reinterpret_cast available... The problem with a char* to double* cast is alignment. It is a type-punned pointer and can fail dereference at runtime if not careful. However, the case here guarantees that the pointer is properly aligned. The default trick is to cast twice, once over void*, to force the compiler to accept the type-pun. |
:-) And no alignas.
You then tricked the compiler, but the cpu could then still not accept the double at that position? To avoid that, should then at linuxcnc/src/hal/classicladder/arrays.c Line 176 in 8b3a6d0 the pByte be initialized as a double* ? |
[snip] On amd64 it does not fail because the pointer is 22 aligned. For arm64 it might be a problem when only on a 22 boundary, but that has not happened either yet. Therefore, I think we are safe here to silence the compiler warning. |
I agree. Maybe just leave a comment? |
This PR has an assortment of fixes for cppcheck detected problems and some are real bugs. Fixes include:
%255s).