marvin-zhao / pyang

Automatically exported from code.google.com/p/pyang
0 stars 0 forks source link

Typedef's default value is not rechecked when the typedef is used as a base type in a leaf #58

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If you define a typedef which has a default value and then use that typedef as 
a base for a type and at the same time you restrict that type in a way which 
makes the default illegal, pyang will never detect it.

To demonstrate:

module typedef-default {
    namespace "http://www.mg-soft.com/ns/typedef-default";
    prefix tdd;
    revision 2011-11-04;
    typedef myuint64 {
        type uint64;
        default 123456789123456789;
    }
    container some-config {
        leaf myuint64leaf {
            type myuint64 {
                range "1..50000000";
            }
        }
    }
}

As you can see the upper range limit is less than what the inherited default 
is. In this case the user must specify a valid default (at least I think that's 
what RFC6020 says or implies). Pyang never reports this.

I'm using pyang-1.2 source and am debugging it in PyDev (python Eclipse plugin) 
on Win7_x64.

The solution would be to modify statements.v_type_leaf(ctx, stmt) function 
where it fails to find a default substatement and then attempts to inherit the 
default from the type's typedef. It is not sufficient to simply inherit, the 
value should also be re-validated using the new type's typespec the same way it 
is in an "if" above this code. Something like this:

stmt.i_default = type_.i_typedef.i_default
stmt.i_default_str = type_.i_typedef.i_default_str
if type_.i_typespec is not None:
    type_.i_type_spec.validate(ctx.errors, stmt.pos,
                        stmt.i_default, ' for the default  value')

I am no python expert though.

RFC6020 7.6.4.
"The value of the "default" statement MUST be valid according to the
type specified in the leaf's "type" statement."
I tought there would be more info in the RFC on the way defaults get propagated 
for leafs (as in the typedef statement's section) so perhaps this is an 
intentional omission in pyang? If so it should be documented.

Original issue reported on code.google.com by jernej.t...@gmail.com on 7 Nov 2011 at 7:40

GoogleCodeExporter commented 9 years ago
I agree that the RFC is not clear enough in this particular case.  As you say, 
it handles new restrictions in typedefs, but not in leafs.

This is a bug in pyang.

Original comment by mbj4...@gmail.com on 7 Nov 2011 at 8:41

GoogleCodeExporter commented 9 years ago

Original comment by mbj4...@gmail.com on 7 Nov 2011 at 8:43