Don't generate array iterators when the length of the array is always 0.
[amitay/samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index 0a4e44ef0ec256301403c4b768a9c32b76e638e0..34aebc7f0fb112265df27386224f101ff13ec7b5 100644 (file)
@@ -586,9 +586,15 @@ sub ParseElementPushLevel
                my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
                my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
 
+               my $array_pointless = ($length eq "0");
+
+               if ($array_pointless) {
+                       warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
+               }
+
                $var_name = get_array_element($var_name, $counter);
 
-               if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
+               if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
                        $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
                        $self->indent;
                        $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
@@ -596,7 +602,7 @@ sub ParseElementPushLevel
                        $self->pidl("}");
                }
 
-               if ($deferred and ContainsDeferred($e, $l)) {
+               if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
                        $self->pidl("for ($counter = 0; $counter < $length; $counter++) {");
                        $self->indent;
                        $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
@@ -857,8 +863,16 @@ sub ParseDataPull($$$$$$$)
 
                if (my $range = has_property($e, "range")) {
                        $var_name = get_value_of($var_name);
+                       my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
                        my ($low, $high) = split(/,/, $range, 2);
-                       $self->pidl("if ($var_name < $low || $var_name > $high) {");
+                       if ($low < 0 and not $signed) {
+                               warning(0, "$low is invalid for the range of an unsigned type");
+                       }
+                       if ($low == 0 and not $signed) {
+                               $self->pidl("if ($var_name > $high) {");
+                       } else {
+                               $self->pidl("if ($var_name < $low || $var_name > $high) {");
+                       }
                        $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
                        $self->pidl("}");
                }
@@ -2165,7 +2179,7 @@ sub ParseFunctionPull($$)
                        if (grep(/in/, @{$e->{DIRECTION}})) {
                                $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
                        } else {
-                               $self->pidl("memset(CONST_DISCARD(struct $fn->{NAME} *,r->out.$e->{NAME}), 0, ($size) * sizeof(*r->out.$e->{NAME}));");
+                               $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
                        }
                } else {
                        $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
@@ -2228,7 +2242,7 @@ sub AuthServiceStruct($$$)
 sub FunctionCallEntry($$)
 {
        my ($self, $d) = @_;
-       return if not defined($d->{OPNUM});
+       return if not defined($d->{OPNUM});
        $self->pidl("\t{");
        $self->pidl("\t\t\"$d->{NAME}\",");
        $self->pidl("\t\tsizeof(struct $d->{NAME}),");
@@ -2237,6 +2251,7 @@ sub FunctionCallEntry($$)
        $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
        $self->pidl("\t\t".($d->{ASYNC}?"true":"false").",");
        $self->pidl("\t},");
+       return 1;
 }
 
 #####################################################################
@@ -2253,8 +2268,7 @@ sub FunctionTable($$)
        $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
 
        foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
-               $self->FunctionCallEntry($d);
-               $count++;
+               $count += $self->FunctionCallEntry($d);
        }
        $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
        $self->pidl("};");
@@ -2540,7 +2554,9 @@ sub GenerateIncludes($)
        if (is_intree()) {
                $self->pidl("#include \"includes.h\"");
        } else {
+               $self->pidl("#ifndef _GNU_SOURCE");
                $self->pidl("#define _GNU_SOURCE");
+               $self->pidl("#endif");
                $self->pidl("#include <stdint.h>");
                $self->pidl("#include <stdlib.h>");
                $self->pidl("#include <stdio.h>");