Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' in the first place of the format field #93184

Open
Pankal7 opened this issue May 24, 2022 · 1 comment
Labels
pending type-bug

Comments

@Pankal7
Copy link

@Pankal7 Pankal7 commented May 24, 2022

Bug report

Using the struct module for socket programming i noticed that when i was using pack command with format 'BHxHH' ,after the message was packed the length was 10 bytes. But the message should be 8 bytes because 'B' is 1 byte, 'H' is 2 bytes and 'x' which is used for padding was 1 byte. After some testing i wrote the command again but changed the order in the format to 'HBxHH'and this time the message was 8 bytes. So i believe there is some issue when 'B' is in the first place of the format field.

Here is a small example:

`from struct import *
import binascii

msg_op = 0
msg_id = 10
msg_number_1 =10
msg_number_2 = 15

message = pack('BHxHH',msg_id,msg_op,msg_number_1,msg_number_2)

print("Hexadecimal representation of data: ",binascii.hexlify(message))

print("length in bytes: ",len(message))

message = pack('HBxHH',msg_op,msg_id,msg_number_1,msg_number_2)

print("Hexadecimal representation of data: ",binascii.hexlify(message))

print("length in bytes: ",len(message))`

And the output is:

Hexadecimal representation of data: b'0a00000000000a000f00'
length in bytes: 10
Hexadecimal representation of data: b'00000a000a000f00'
length in bytes: 8

Environment

  • CPython versions tested on: 3.9.7
  • Operating system and architecture: Microsoft Windows 10 Home (x64-based processor)
@Pankal7 Pankal7 added the type-bug label May 24, 2022
@Pankal7 Pankal7 changed the title Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' first in format Socket programming using struct module , pack function produces wrong amount of bytes while using 'B' in the first place of the format field May 24, 2022
@sweeneyde
Copy link
Member

@sweeneyde sweeneyde commented May 25, 2022

See the struct module docs:

Note By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details.

@sweeneyde sweeneyde added the pending label May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending type-bug
Projects
None yet
Development

No branches or pull requests

2 participants