[PATCH] Minor error in serial_configure

Jef Driesen jef at libdivecomputer.org
Wed May 14 01:49:51 PDT 2014


On 2014-05-13 13:37, Jef Driesen wrote:
> On 2014-05-13 11:39, Venkatesh Shukla wrote:
>> I have attached a patch to a minute mistake that I noticed in the 
>> code.
>> 
>> -	if (memcmp (&tty, &active, sizeof (struct termios) != 0)) {
>> +	if (memcmp (&tty, &active, sizeof (struct termios)) !=0) {
> 
> Nice catch! I'll apply the patch.
> 
> Due to this bug, the memcmp only checked the first byte. I wonder
> whether fixing this will have any negative side effect, with this
> discussion in mind:
> 
> http://libdivecomputer.org/pipermail/devel/2014-March/000268.html
> 
> Anyway, we'll find out soon enough.

Great, I already discovered a major problem. After applying this patch, 
the memcmp always fails on my system. Not good.

The problem seems to be that the termios struct contains some padding 
bytes on my system. Because the content of padding bytes is unspecified, 
they may contain some random data, which causes the memcmp to fail. 
Comparing the individual termios members, which would be the portable 
way to compare structs, isn't an option here, because the contents of 
the termios struct is platform dependent.

Explicitly initializing the termios structure with memset, will also set 
the padding bytes to zero. This seems to work fine on my system. But the 
C standard doesn't make any guarantee regarding padding bytes. Thus in 
theory the compiler is free to change those padding values at will. I 
doubt this will be a problem in practice, but just in case, please test 
the attached patch.

The alternative solution is removing the memcmp check completely.

Jef
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Initialize-the-termios-structure.patch
Type: text/x-diff
Size: 1393 bytes
Desc: not available
URL: <http://libdivecomputer.org/pipermail/devel/attachments/20140514/14ce40b1/attachment.patch>


More information about the devel mailing list