Also decode strings in _DynamicStringTable.get_string() (#217)
authorAndreas Ziegler <ziegler@einserver.de>
Sat, 16 Feb 2019 13:25:59 +0000 (14:25 +0100)
committerEli Bendersky <eliben@users.noreply.github.com>
Sat, 16 Feb 2019 13:25:59 +0000 (05:25 -0800)
StringTableSection.get_string() returns an UTF-8 decoded
string (or '' if fetching the string failed) since #182
but the code in _DynamicStringTable was never updated to
decode anything at all so it just returns a bytes sequence
in Python 3.

Let's convert the string there as well to be able to use
both string tables the same way without having to worry
about decoding. Adapt the test cases accordingly.

elftools/elf/dynamic.py
test/test_dynamic.py

index 9282284437371c522798dd9955bf68f1ef1eff54..e75c16ed935dfd4c4bf9bebb4202547fbb653b1f 100644 (file)
@@ -25,8 +25,8 @@ class _DynamicStringTable(object):
     def get_string(self, offset):
         """ Get the string stored at the given offset in this string table.
         """
-        return parse_cstring_from_stream(self._stream,
-                                         self._table_offset + offset)
+        s = parse_cstring_from_stream(self._stream, self._table_offset + offset)
+        return s.decode('utf-8') if s else ''
 
 
 class DynamicTag(object):
index 1ef00809c45d695e59a36dfe59bfb5b0e14137b1..c55fc2e3b95f55c3f829fdaeb5e7ae1664cd9066 100644 (file)
@@ -49,7 +49,7 @@ class TestDynamic(unittest.TestCase):
 
                 for t in segment.iter_tags():
                     if t.entry.d_tag == 'DT_NEEDED':
-                        libs.append(t.needed.decode('utf-8'))
+                        libs.append(t.needed)
 
         exp = ['libc.so.6']
         self.assertEqual(libs, exp)
@@ -65,7 +65,7 @@ class TestDynamic(unittest.TestCase):
 
                 symbol_names = [x.name for x in segment.iter_symbols()]
 
-        exp = [b'', b'__libc_start_main', b'__gmon_start__', b'abort']
+        exp = ['', '__libc_start_main', '__gmon_start__', 'abort']
         self.assertEqual(symbol_names, exp)
 
     def test_sunw_tags(self):