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

encoding/asn1: weird error handling for parseBitString #29908

Closed
gypsyfeng opened this issue Jan 23, 2019 · 2 comments
Closed

encoding/asn1: weird error handling for parseBitString #29908

gypsyfeng opened this issue Jan 23, 2019 · 2 comments

Comments

@gypsyfeng
Copy link

@gypsyfeng gypsyfeng commented Jan 23, 2019

https://github.com/golang/go/blob/3813edf26edb78620632dc9c7d66096e5b2b5019/src/encoding/asn1/asn1.go

For the following code, error handling "invalid padding bits in BIT STRING" is confusing and doesn't look right. Many bitmap is just one byte long and definitely bigger than 0. Maybe I understand asn1's bit string wrong.

func parseBitString(bytes []byte) (ret BitString, err error) {
	if len(bytes) == 0 {
		err = SyntaxError{"zero length BIT STRING"}
		return
	}
	paddingBits := int(bytes[0])
	if paddingBits > 7 ||
		len(bytes) == 1 && paddingBits > 0 ||
		bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
		err = SyntaxError{"invalid padding bits in BIT STRING"}
		return
	}
	ret.BitLength = (len(bytes)-1)*8 - paddingBits
	ret.Bytes = bytes[1:]
	return
}
@bcmills
Copy link
Member

@bcmills bcmills commented Jan 29, 2019

CC @FiloSottile @agl for encoding/asn1.

Please provide more detail: do you have an example of a specific input that produces an incorrect or misleading error?

Per the description at http://luca.ntop.org/Teaching/Appunti/asn1.html:

In a primitive encoding, the first contents octet gives the number of bits by which the length of the bit string is less than the next multiple of eight (this is called the "number of unused bits"). The second and following contents octets give the value of the bit string, converted to an octet string.

The condition in the code rejects:

  • paddingBits > 7 (a number of bits that exceeds an octet)
  • len(bytes) == 1 && paddingBits > 0 (a nonzero number of bits when no bits remain in the string)
  • bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0, padding bits filled with a value other than 0.

Which of those “doesn't look right”, or what's missing?

@bcmills bcmills changed the title asn1 package: wired error handling for parseBitString encoding/asn1: weird error handling for parseBitString Jan 29, 2019
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 1, 2019

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Mar 1, 2019
@golang golang locked and limited conversation to collaborators Feb 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.