Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7273] struct.pack adding extra '\x00' character in very specific case

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 6, 2009, 8:16 AM

Post #1 of 2 (58 views)
Permalink
[issue7273] struct.pack adding extra '\x00' character in very specific case

New submission from Caleb Madrigal <caleb.madrigal[at]gmail.com>:

struct.pack("17scBH", 'a'*17, 'c', 255, 65535)
produces 'aaaaaaaaaaaaaaaaac\xff\x00\xff\xff'.

Notice the extra '\x00' character between '\xff' and '\xff\xff'.

I have noticed that this happens when there is an odd-length string
(like '17s'), followed by a character ('c'), followed by a byte ('B'),
followed by an unsigned short ('H').

Other variations reproduce it also (such as using a 19-character string).

However, if any one of the following modifications are made to the
format, the problem goes away:
* Change the string to 16s
* Remove the byte ('B')
* Remove the character ('c')
* Remove the unsigned short ('H')

So obviously, this is a seriously deep, dark corner-case.

----------
components: Library (Lib)
messages: 94980
nosy: cmadrigal
severity: normal
status: open
title: struct.pack adding extra '\x00' character in very specific case
type: behavior
versions: Python 2.5, Python 2.6, Python 3.0

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue7273>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 6, 2009, 8:25 AM

Post #2 of 2 (47 views)
Permalink
[issue7273] struct.pack adding extra '\x00' character in very specific case [In reply to]

Mark Dickinson <dickinsm[at]gmail.com> added the comment:

I don't think this is a bug: struct.pack deliberately adds padding bytes
so that the byte sequence matches the way that a corresponding C struct
would be stored in memory, on that platform. This is described
(admittedly rather briefly) in the documentation at:

http://docs.python.org/library/struct.html

"""By default, C numbers are represented in the machine’s native format
and byte order, and properly aligned by skipping pad bytes if necessary
(according to the rules used by the C compiler)."""

Depending on your application, you may want to use standard size and
alignment instead, e.g., with:

struct.pack("<17scBH", ...)

----------
assignee: -> mark.dickinson
nosy: +mark.dickinson
resolution: -> invalid
status: open -> closed

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue7273>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.