mbj4668 / pyang

An extensible YANG validator and converter in python
ISC License
535 stars 344 forks source link

Crash because ModSubmodStatement.internal_reset() doesn't initialize i_is_primary_module #802

Closed wlupton closed 2 years ago

wlupton commented 2 years ago

Thanks to @jboyd77 for pointing out this problem, and for the fix (to be proposed via a pull request).

ModSubmodStatement.internal_reset() gets called as a consequence of module.internal_reset() after transforms have executed. It calls Statement.internal_reset(), which deletes all self.i_xxx attributes and then sets self.i_is_validated to False. However (unlike the constructor) it doesn't initialize self.i_is_primary_module.

This causes a crash when (a) using transforms, and (b) the module imports submodules. For example, with this YANG:

module reset-bug {
  namespace "urn:reset-bug";
  prefix reset-bug;
  include reset-bug-base;
}
submodule reset-bug-base {
  belongs-to reset-bug {
    prefix reset-bug;
  }
}

...we get this:

% pyang reset-bug.yang -t edit
Traceback (most recent call last):
  File "/Users/william/Development/pyang/default/bin/pyang", line 580, in <module>
    run()
  File "/Users/william/Development/pyang/default/bin/pyang", line 468, in run
    ctx_validate_and_prune()
  File "/Users/william/Development/pyang/default/bin/pyang", line 429, in ctx_validate_and_prune
    ctx.validate()
  File "/Users/william/Development/pyang/default/pyang/context.py", line 348, in validate
    statements.validate_module(self, m)
  File "/Users/william/Development/pyang/default/pyang/statements.py", line 414, in validate_module
    iterate(module, phase)
  File "/Users/william/Development/pyang/default/pyang/statements.py", line 371, in iterate
    res = f(ctx, stmt)
  File "/Users/william/Development/pyang/default/pyang/statements.py", line 190, in <lambda>
    ('import', 'module'):lambda ctx, s: v_import_module(ctx, s),
  File "/Users/william/Development/pyang/default/pyang/statements.py", line 617, in v_import_module
    submodule = add_module(i, stmt.i_is_primary_module)
AttributeError: i_is_primary_module