Skip Menu |

This queue is for tickets about the Convert-Binary-C CPAN distribution.

Report information
The Basics
Id: 3753
Status: resolved
Priority: 0/
Queue: Convert-Binary-C

People
Owner: MHX [...] cpan.org
Requestors: waterman [...] clinic.net
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.45
Fixed in: (no value)



Subject: packing a struct/member/field causes a bur error
I'm using perl ver 5.6.1. I have a C struct with members with fields, i.e., " use Convert::Binary::C; #--------------------------------------------- # Create a new object and parse embedded code #--------------------------------------------- my $c = Convert::Binary::C->new->parse( <<ENDC ); enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; struct Date { int year : 32; enum Month month : 8; int day : 32; }; ENDC #----------------------------------------------- # Pack Perl data structure into a binary string #----------------------------------------------- my $date = { year => 2002, month => 'DEC', day => 24 }; my $packed = $c->pack( 'Date', $date ); " The call to $c->pack causes a bus error. If I remove the " : " operators from the struct members, the call to pack works fine. The above example is a simpilfied version of the actual C code that caused the problem. I hope this gives you enough info to reproduce the problem. Please let me know if I can help as I have been programming for over 25 years. Regards, Bill Waterman
[guest - Mon Sep 8 18:36:58 2003]: Show quoted text
> The call to $c->pack causes a bus error. If I remove the " : " > operators from the struct members, the call to pack works fine.
Yes, I can easily reproduce this. However, the main problem is that bitfields are not yet implemented. Why the above code doesn't work is clearly stated in the manual: " Bitfields are currently not supported by Convert::Binary::C, because I generally don't use them. I plan to support them in a later release, when I will have found an easy way of integrating them into the module. [...] While bitfields are not appropriately handled by the conversion routines yet, they are already parsed correctly. [...] Also, the size of a structure containing bitfields is not valid, as bitfields internally do not increase the size of a structure yet. " According to the above, the size of a struct only containing bitfield members is zero. This zero-size struct triggers a very obscure but easy to understand bug in my code that I will have fixed in the next release (0.46, expect it within a few days). But this will still not make the above example work, it will just not give you a bus error any longer. Besides, if you would have run your example with Perl warnings turned on (-w), it would have shown you the following warning before the bus error: "Bitfields are unsupported in pack('Date') at test.pl line 25." The good news is that I'm currently working on adding basic support for bitfields to Convert::Binary::C, so maybe this is going to work in a couple of weeks/months. -- Marcus
[waterman@clinic.net - Wed Sep 10 06:08:28 2003]: Show quoted text
> Thank you for the prompt response. You are correct, the module works as > advertised. In my rush to try it out, I didn't read all of the details. > The devil is in the details. I was using C++ code... is that OK? Also, I
Yes and no. The parser only supports ANSI-C (with some enhancements I made towards parsing ISO-C99), so parsing C++ (which is a lot more complex) is not possible. However, I don't see an actual need to be able to parse C++, as 1) the only stuff you want to use with Convert::Binary::C is plain structs, unions, typedefs. You wouldn't want to unpack any C++ specific stuff, e.g. classes. 2) you can always hide the C++ parts of your header files in #ifdef __cplusplus [...] #endif But you're welcome to contribute a drop-in C++ grammar... :) Show quoted text
> had to move some typedef's outside of the struct to avoid a parse error > ( unexpected token I think). Can you make a typedef a valid token inside > of a struct?
This is also a C++ specific feature. You won't get this compiled with an ANSI-C compiler either. The problem is that in C++ a struct does not differ in very much more than the name from a class, and within a class you are allowed to do quite a lot of stuff that you aren't allowed to do in ANSI/ISO-C. Show quoted text
> I think your module is a fine peice of code. If you're not a programmer, > you would make an execllent one. The documentation is execllent.
I constantly try to improve the module. However, since it's a hobby project, I often can't contribute as much time as I would like. -- Marcus