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

SMS-DELIVER from number decoding error #33

Closed
9dogs opened this issue Mar 6, 2017 · 6 comments
Closed

SMS-DELIVER from number decoding error #33

9dogs opened this issue Mar 6, 2017 · 6 comments

Comments

@9dogs
Copy link

9dogs commented Mar 6, 2017

Hey, I've encountered strange bug while decoding some pdu's. I got EncodingError exception on some messages while the others are decoding normally. It seems that a problem is in a function nibble2octet:

def nibble2octet(o):
    if o % 2:
        return o / 2 + 1
    else:
        return o / 2

In Python 3 this function can produce float values. The value is used later in unpackSeptets and comparison with int breaks desired behavior. If it is simple python2->python3 migration issue, it can be fixed simply by return int(o / 2) + 1. Can somebody confirm that fix doesn't break anything else?

@tomchy
Copy link

tomchy commented Mar 6, 2017

Back in the faucamp/python-gsmmodem repository instead of nibble2octet(o) there were two pieces of code doing this operation:

addressLen = int(math.ceil(addressLen / 2.0))

and:

if addressLen % 2:
    addressLen = int(addressLen / 2) + 1
else:
    addressLen = int(addressLen / 2)

There are no more occurrences of nibble2octet, so I think that confirms that this fix doesn't break anything else 🙂

@9dogs
Copy link
Author

9dogs commented Mar 6, 2017

Thank you! I'm making pull request then.

@lcnittl
Copy link
Collaborator

lcnittl commented Mar 6, 2017

Just a question: Shouldn't addressLen = int((addressLen + 1) / 2) give the same values, just without the need of an if statement or a function from math?

@babca
Copy link
Owner

babca commented Mar 6, 2017

The faucamp@df20a9a fix from 2014 was originally part of the codebase until I merged @paolo-losi branch.

The nibble2octet() function was introduced by paolo-losi@0076b9d to fix something. I think he wanted to use the same piece of code on both places and he forgot to use int() in his new function.

@babca
Copy link
Owner

babca commented Mar 6, 2017

quick test, all three blocks give the same values:

0
1
1
2
2
3
3
import math

def testblock1(addressLen):
	if addressLen % 2:
	    addressLen = int(addressLen / 2) + 1
	else:
	    addressLen = int(addressLen / 2)
	print (addressLen)

testblock1(0)
testblock1(1)
testblock1(2)
testblock1(3)
testblock1(4)
testblock1(5)
testblock1(6)

def testblock2(addressLen):
	addressLen = int(math.ceil(addressLen / 2.0))
	print (addressLen)

testblock2(0)
testblock2(1)
testblock2(2)
testblock2(3)
testblock2(4)
testblock2(5)
testblock2(6)

def testblock3(addressLen):
	addressLen = int((addressLen + 1) / 2)
	print (addressLen)

testblock3(0)
testblock3(1)
testblock3(2)
testblock3(3)
testblock3(4)
testblock3(5)
testblock3(6)

@babca
Copy link
Owner

babca commented Mar 6, 2017

I suggest to change nibble2octet() function to:

def nibble2octet(addressLen):
    return int((addressLen + 1) / 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants