lightware_laser_serial: fix pointer for enabling serial mode #21747
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Solved Problem
When trying to fix #21737 (review) and compiling
make beaglebone_blue
locally witharm-linux-gnueabihf-gcc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
I got a compile warning -> error:Solution
const char *data = "www\r\n";
Defines a cstring of 6 bytes:
{'w', 'w', 'w', '\r', '\n', '\0'}
type of data:
char const*
type of &data:
char const**
So when we call
write(_fd, &data, strlen(data));
thenstrlen(data) == 5
(without the terminating null character) and we send the 4 byte memory address of data + some additional random byte to the distance sensor.Correct is
write(_fd, data, strlen(data));
wherechar const*
gets cast toconst void *
and we pass the pointer to the content of data so the 5 bytes w, w, w, \r, \n get sent out via serial.The fundamental problem here is write() not being typesafe.
Changelog Entry
For release notes:
Alternatives
would be correct as well but creates slightly more overhead since it makes a copy of the cstring on the stack and passes the pointer of that to
write()
.Test coverage