sepolgen: Unicode-objects must be encoded before hashing.

sha256 hash operates with bytes and in Python3 all strings are unicode
by default, we must encode the data before hashing to ensure they
are bytes in Python3

Signed-off-by: Robert Kuska <rkuska@redhat.com>
This commit is contained in:
Robert Kuska 2015-07-16 13:48:15 +02:00 committed by Stephen Smalley
parent 788f5dba54
commit aee172010a
2 changed files with 20 additions and 4 deletions

View File

@ -76,6 +76,20 @@ def first(s, sorted=False):
for x in s:
return x
def encode_input(text):
import locale
"""Encode given text via preferred system encoding"""
# locale will often find out the correct encoding
encoding = locale.getpreferredencoding()
try:
encoded_text = text.encode(encoding)
except UnicodeError:
# if it fails to find correct encoding then ascii is used
# which may lead to UnicodeError if `text` contains non ascii signs
# utf-8 is our guess to fix the situation
encoded_text = text.encode('utf-8')
return encoded_text
if __name__ == "__main__":
import sys
import time

View File

@ -69,6 +69,8 @@ error_count = 3 # Number of symbols that must be shifted to leave
import re, types, sys, cStringIO, hashlib, os.path
from . import util
# Exception raised for yacc-related errors
class YaccError(Exception): pass
@ -1962,7 +1964,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module,
# Add parsing method to signature
Signature.update(method)
Signature.update(util.encode_input(method))
# If a "module" parameter was supplied, extract its dictionary.
# Note: a module may in fact be an instance as well.
@ -1995,7 +1997,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module,
if not start:
start = ldict.get("start",None)
if start:
Signature.update(start)
Signature.update(util.encode_input(start))
# If running in optimized mode. We're going to
@ -2064,7 +2066,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module,
if not (isinstance(prec,types.ListType) or isinstance(prec,types.TupleType)):
raise YaccError,"precedence must be a list or tuple."
add_precedence(prec)
Signature.update(repr(prec))
Signature.update(util.encode_input(repr(prec)))
for n in tokens:
if not Precedence.has_key(n):
@ -2112,7 +2114,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module,
# Make a signature of the docstrings
for f in symbols:
if f.__doc__:
Signature.update(f.__doc__)
Signature.update(util.encode_input(f.__doc__))
lr_init_vars()