Skip to content

Commit c0b2feb

Browse files
author
cabrol
committed
Corrections to commentAttributes parsing logic
*minor cstruct.py fixes *added functioning test to verify comments parsing Signed-off-by: qcabrol
1 parent 719ead5 commit c0b2feb

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

dissect/cstruct/cstruct.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ def _parse_fields(self, s):
412412
if d['type'].startswith('//'):
413413
continue
414414

415+
commentAttributes={}
416+
415417
#parse the comment header
416418
if d['commentBlock'] is not None and d['commentBlock'].startswith('/*'):
417419
commentfields = re.finditer(
@@ -447,7 +449,7 @@ def _parse_fields(self, s):
447449
d['name'] = d['name'][1:]
448450
type_ = Pointer(self.cstruct, type_)
449451

450-
field = Field(d['name'], type_, int(d['bits']) if d['bits'] else None, commentAttributes)
452+
field = Field(d['name'], type_, int(d['bits']) if d['bits'] else None, commentAttributes=commentAttributes)
451453
r.append(field)
452454

453455
return r
@@ -761,6 +763,7 @@ def __init__(self, cstruct, name, fields=None):
761763
self.lookup = OrderedDict()
762764
self.fields = fields if fields else []
763765

766+
764767
for f in self.fields:
765768
self.lookup[f.name] = f
766769

@@ -983,15 +986,15 @@ def reset(self):
983986
class Field(object):
984987
"""Holds a structure field."""
985988

986-
def __init__(self, name, type_, bits=None, offset=None, commentAttributes=None):
989+
def __init__(self, name, type_, bits=None, offset=None, commentAttributes={}):
987990
self.name = name
988991
self.type = type_
989992
self.bits = bits
990993
self.offset = offset
991994
self.commentAttributes = commentAttributes
992995

993996
def __repr__(self):
994-
return '<Field {} {}>'.format(self.name, self.type)
997+
return '<Field {} {} {}>'.format(self.name, self.type, self.commentAttributes)
995998

996999

9971000
class Array(BaseType):

tests/test_basic.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,37 @@ def test_dumpstruct(capsys):
770770
captured_2 = capsys.readouterr()
771771

772772
assert captured_1.out == captured_2.out
773+
774+
775+
def test_commentfieldparse(capsys):
776+
c = cstruct.cstruct()
777+
c.load("""
778+
/*discardedCom1*/
779+
struct test{
780+
/*
781+
* @scale: 0.001
782+
* @unit: testUnit1
783+
*/
784+
int testVar1;
785+
int testVar2;
786+
/* dicardedCom2
787+
* @scale: 5
788+
* @unit: testUnit2
789+
*/
790+
int testVar3;
791+
};
792+
""", compiled=False)
793+
794+
assert c.test.name == 'test'
795+
796+
assert 'testVar1' in c.test.lookup
797+
assert 'testVar2' in c.test.lookup
798+
assert 'testVar2' in c.test.lookup
799+
800+
assert c.test.lookup['testVar1'].commentAttributes['scale'] == '0.001'
801+
assert c.test.lookup['testVar1'].commentAttributes['unit'] == 'testUnit1'
802+
803+
assert c.test.lookup['testVar2'].commentAttributes == {}
804+
805+
assert c.test.lookup['testVar3'].commentAttributes['scale'] == '5'
806+
assert c.test.lookup['testVar3'].commentAttributes['unit'] == 'testUnit2'

0 commit comments

Comments
 (0)