pidl:NDR/Parser: fix range() for arrays
[samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index 83bca288edee8bc760cb82b5943cd48849031382..02f7c9a49bb3d64a096519c2e930bc364ed6f40b 100644 (file)
@@ -870,7 +870,10 @@ sub ParseDataPull($$$$$$$)
 
                $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
 
-               if (my $range = has_property($e, "range")) {
+               my $pl = GetPrevLevel($e, $l);
+
+               my $range = has_property($e, "range");
+               if ($range and $pl->{TYPE} ne "ARRAY") {
                        $var_name = get_value_of($var_name);
                        my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
                        my ($low, $high) = split(/,/, $range, 2);
@@ -1015,6 +1018,20 @@ sub ParseElementPullLevel
                } elsif ($l->{TYPE} eq "ARRAY") {
                        my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
 
+                       if (my $range = has_property($e, "range")) {
+                               my ($low, $high) = split(/,/, $range, 2);
+                               if ($low < 0) {
+                                       warning(0, "$low is invalid for the range of an array size");
+                               }
+                               if ($low == 0) {
+                                       $self->pidl("if ($length > $high) {");
+                               } else {
+                                       $self->pidl("if ($length < $low || $length > $high) {");
+                               }
+                               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+                               $self->pidl("}");
+                       }
+
                        my $nl = GetNextLevel($e, $l);
 
                        if (is_charset_array($e,$l)) {
@@ -1083,6 +1100,20 @@ sub ParseElementPullLevel
                        $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
                }
 
+               if (my $range = has_property($e, "range")) {
+                       my ($low, $high) = split(/,/, $range, 2);
+                       if ($low < 0) {
+                               warning(0, "$low is invalid for the range of an array size");
+                       }
+                       if ($low == 0) {
+                               $self->pidl("if ($length > $high) {");
+                       } else {
+                               $self->pidl("if ($length < $low || $length > $high) {");
+                       }
+                       $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+                       $self->pidl("}");
+               }
+
                $var_name = get_array_element($var_name, $counter);
 
                $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
@@ -1466,6 +1497,7 @@ sub ParseStructPrint($$$$$)
        $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
 
        $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
+       $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
 
        $self->start_flags($struct, $ndr);
 
@@ -2039,6 +2071,7 @@ sub ParseFunctionPrint($$)
        }
 
        $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
+       $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
        $self->pidl("$ndr->depth++;");
 
        $self->pidl("if (flags & NDR_SET_VALUES) {");