tests/driver_at86rf2xx: fix var lengths and unused vars#7860
tests/driver_at86rf2xx: fix var lengths and unused vars#7860miri64 merged 1 commit intoRIOT-OS:masterfrom
Conversation
tests/driver_at86rf2xx/cmd.c
Outdated
| int res; | ||
| netdev_ieee802154_t *dev; | ||
| const size_t count = 2; /* mhr + payload */ | ||
| size_t count = 2; /* mhr + payload */ |
There was a problem hiding this comment.
How about static const instead?
There was a problem hiding this comment.
Well clang was complaining about the const so that's why I changed, since IMHO there's not necessary. What static would change?
There was a problem hiding this comment.
Static const means it won't take up stack space
There was a problem hiding this comment.
Well I get the original error if I change it:
/Users/facosta/git/RIOT-OS/RIOT/tests/driver_at86rf2xx/cmd.c:253:25: error: variable length array folded to constant array as an extension
[-Werror,-Wgnu-folding-constant]
struct iovec vector[count];
^~~~~
1 error generated.There was a problem hiding this comment.
Can you make it a macro constant then instead?
#define MAC_VECTOR_SIZE (2)Doesn't make much sense to use stack space for that constant anyway.
|
Well, cppcheck complains about something I was noticed already, but have no clue how it should be solved. Any ideas? |
| uint8_t addr[_MAX_ADDR_LEN]; | ||
| int iface, idx = 2, res; | ||
| int iface, idx = 2; | ||
| size_t res; |
There was a problem hiding this comment.
Why did you need to change this? The parse function returns int
There was a problem hiding this comment.
Because of this:
/Users/facosta/git/RIOT-OS/RIOT/tests/driver_at86rf2xx/cmd.c:160:36: error: comparison of integers of different signs: 'int' and
'unsigned long' [-Werror,-Wsign-compare]
if ((res <= 0) || (res > sizeof(pan))) {
~~~ ^ ~~~~~~~~~~~
1 error generated.There was a problem hiding this comment.
It would always make more sense to me to change the comparison than, instead of the type.
tests/driver_at86rf2xx/cmd.c
Outdated
| int res; | ||
| netdev_ieee802154_t *dev; | ||
| const size_t count = 2; /* mhr + payload */ | ||
| size_t count = 2; /* mhr + payload */ |
There was a problem hiding this comment.
Can you make it a macro constant then instead?
#define MAC_VECTOR_SIZE (2)Doesn't make much sense to use stack space for that constant anyway.
| od_hex_dump(buffer + mhr_len, data_len - mhr_len, 0); | ||
| printf("txt: "); | ||
| for (int i = mhr_len; i < data_len; i++) { | ||
| for (size_t i = mhr_len; i < data_len; i++) { |
There was a problem hiding this comment.
Use rather unsigned as a counting variable.
There was a problem hiding this comment.
The change here is because the comparison shouldn't be made between two variables of different size. mhr_len and data_len are declared as size_t.
There was a problem hiding this comment.
a) where did you get that from? Comparisons between variables of different size shouldn't be a problem at all
b) I think on all our platforms sizeof(size_t) == sizeof(unsigned) is true.
There was a problem hiding this comment.
a) where did you get that from? Comparisons between variables of different size shouldn't be a problem at all
I said they shouldn't, because it might lead to errors, if a counter variable is smaller than the variable it's compared with of course it cannot reach the required length. It will only work if the count doesn't exceed it's own type length (which yes, it's mostly what happens).
b) I think on all our platforms sizeof(size_t) == sizeof(unsigned) is true.
I think it's not enough, our code needs to be portable. sizeof() returns size_t, you can find here a small discussion about it.
miri64
left a comment
There was a problem hiding this comment.
Well, cppcheck complains about something I was noticed already, but have no clue how it should be solved. Any ideas?
Since you changed the type of res to size_t (which is an unsigned type) it doesn't make sense to check if it is res is less than 0. That is what cppcheck is complaining about. I guess it wasn't a good idea to change that type after all, so let's go with casting to unsigned for the comparisons clang is complaining about.
Then I think we're doing something wrong here, the compiler is stating the obvious, so I'd prefer to make the right comparisons rather than trick the compiler to avoid the warning. |
if a signed value is on the side that is supposed to be smaller, (e.g. |
I agree, but why not just do what's correct? Just compare two variables of the same length and no compiler will complain. |
Because it wastes memory unnecessarily. Comparing two values of different sizes is still correct if you know what you are doing. |
This optimisation trick might lead to errors. I prefer to waste 2B for a correct type and length than "expect" we all know what we're doing. I believe compiler warnings are not thrown just for fun... |
It's not a trick. But you are changing types instead of operation which definitely leads to errors. |
|
(see murdock output) |
30b3188 to
d834853
Compare
36c0126 to
3dfd2ac
Compare
3dfd2ac to
b072c90
Compare
b072c90 to
129c170
Compare
While compiling tests/driver_at86rf2xx with clang in OS X it discovers several minor issues.
This PR fixes them.