pidl:Samba4/NDR/Parser: fix NDR64 union alignment
[kai/samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Parser.pm
index af6885f67a6815d94cd89c4485a2fee8168471d5..09518f7ceec94d45b790e137583f3c0bd974adb9 100644 (file)
@@ -204,12 +204,12 @@ sub ParseArrayPushHeader($$$$$$)
        }
 
        if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
+               $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
        }
        
        if ($l->{IS_VARYING}) {
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));");  # array offset
-               $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $length));");
+               $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));");  # array offset
+               $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
        } 
 
        return $length;
@@ -326,7 +326,7 @@ sub ParseArrayPullHeader($$$$$$)
 
        if ($l->{IS_CONFORMANT}) {
                $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
-       } elsif ($l->{IS_ZERO_TERMINATED}) { # Noheader arrays
+       } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
                $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
        } else {
                $length = $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
@@ -567,17 +567,24 @@ sub ParseElementPushLevel
        }
 
        if ($l->{TYPE} eq "POINTER" and $deferred) {
+               my $rel_var_name = $var_name;
                if ($l->{POINTER_TYPE} ne "ref") {
                        $self->pidl("if ($var_name) {");
                        $self->indent;
                        if ($l->{POINTER_TYPE} eq "relative") {
-                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2($ndr, $var_name));");
+                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
+                       }
+                       if ($l->{POINTER_TYPE} eq "relative_short") {
+                               $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));");
                        }
                }
                $var_name = get_value_of($var_name);
                $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
 
                if ($l->{POINTER_TYPE} ne "ref") {
+                       if ($l->{POINTER_TYPE} eq "relative") {
+                               $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
+                       }
                        $self->deindent;
                        $self->pidl("}");
                }
@@ -586,9 +593,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 +609,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);
@@ -664,6 +677,8 @@ sub ParsePtrPush($$$$$)
                }
        } elsif ($l->{POINTER_TYPE} eq "relative") {
                $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
+       } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+               $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));");
        } elsif ($l->{POINTER_TYPE} eq "unique") {
                $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
        } elsif ($l->{POINTER_TYPE} eq "full") {
@@ -776,9 +791,6 @@ sub ParseElementPrint($$$$$)
                                $self->pidl("$ndr->depth++;");
                                $self->pidl("for ($counter=0;$counter<$length;$counter++) {");
                                $self->indent;
-                               $self->pidl("char *idx_$l->{LEVEL_INDEX}=NULL;");
-                               $self->pidl("if (asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter) != -1) {");
-                               $self->indent;
 
                                $var_name = get_array_element($var_name, $counter);
                        }
@@ -801,9 +813,6 @@ sub ParseElementPrint($$$$$)
                } elsif (($l->{TYPE} eq "ARRAY")
                        and not is_charset_array($e,$l)
                        and not has_fast_array($e,$l)) {
-                       $self->pidl("free(idx_$l->{LEVEL_INDEX});");
-                       $self->deindent;
-                       $self->pidl("}");
                        $self->deindent;
                        $self->pidl("}");
                        $self->pidl("$ndr->depth--;");
@@ -855,7 +864,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);
@@ -933,12 +945,10 @@ sub ParseMemCtxPullFlags($$$$)
 
        if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
                my $nl = GetNextLevel($e, $l);
-               my $next_is_array = ($nl->{TYPE} eq "ARRAY");
-               my $next_is_string = (($nl->{TYPE} eq "DATA") and 
-                                       ($nl->{DATA_TYPE} eq "string"));
-               if ($next_is_array or $next_is_string) {
-                       return undef;
-               } elsif ($l->{LEVEL} eq "TOP") {
+               return undef if ($nl->{TYPE} eq "ARRAY");
+               return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string"));
+
+               if ($l->{LEVEL} eq "TOP") {
                        $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
                }
        }
@@ -1000,6 +1010,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)) {
@@ -1032,7 +1056,7 @@ sub ParseElementPullLevel
                        $self->pidl("if ($var_name) {");
                        $self->indent;
 
-                       if ($l->{POINTER_TYPE} eq "relative") {
+                       if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
                                $self->pidl("uint32_t _relative_save_offset;");
                                $self->pidl("_relative_save_offset = $ndr->offset;");
                                $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
@@ -1047,7 +1071,12 @@ sub ParseElementPullLevel
                $self->ParseMemCtxPullEnd($e, $l, $ndr);
 
                if ($l->{POINTER_TYPE} ne "ref") {
-                       if ($l->{POINTER_TYPE} eq "relative") {
+                       if ($l->{POINTER_TYPE} eq "relative") {
+                               $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {");
+                               $self->indent;
+                               $self->pidl("$ndr->relative_highest_offset = $ndr->offset;");
+                               $self->deindent;
+                               $self->pidl("}");
                                $self->pidl("$ndr->offset = _relative_save_offset;");
                        }
                        $self->deindent;
@@ -1059,6 +1088,24 @@ sub ParseElementPullLevel
                my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
                my $array_name = $var_name;
 
+               if ($l->{IS_VARYING}) {
+                       $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);
@@ -1155,6 +1202,8 @@ sub ParsePtrPull($$$$$)
                 ($l->{POINTER_TYPE} eq "relative") or
                 ($l->{POINTER_TYPE} eq "full")) {
                $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
+       } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+               $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));");
        } else {
                die("Unhandled pointer type $l->{POINTER_TYPE}");
        }
@@ -1175,7 +1224,7 @@ sub ParsePtrPull($$$$$)
        }
 
        #$self->pidl("memset($var_name, 0, sizeof($var_name));");
-       if ($l->{POINTER_TYPE} eq "relative") {
+       if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
                $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
        }
        $self->deindent;
@@ -1210,9 +1259,9 @@ sub ParseStructPushPrimitives($$$$$)
                                $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
                        }
 
-                       $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));");
+                       $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
                } else {
-                       $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
+                       $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
                }
        }
 
@@ -1225,6 +1274,8 @@ sub ParseStructPushPrimitives($$$$$)
        }
 
        $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
+
+       $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));");
 }
 
 sub ParseStructPushDeferred($$$$)
@@ -1250,7 +1301,7 @@ sub ParseStructPush($$$$)
 
        EnvSubstituteValue($env, $struct);
 
-       $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
+       $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
 
        $self->start_flags($struct, $ndr);
 
@@ -1277,7 +1328,7 @@ sub ParseEnumPush($$$$)
        my($type_fn) = $enum->{BASE_TYPE};
 
        $self->start_flags($enum, $ndr);
-       $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
+       $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));");
        $self->end_flags($enum, $ndr);
 }
 
@@ -1291,7 +1342,7 @@ sub ParseEnumPull($$$$)
 
        $self->pidl("$type_v_decl v;");
        $self->start_flags($enum, $ndr);
-       $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
+       $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));");
        $self->pidl("*$varname = v;");
 
        $self->end_flags($enum, $ndr);
@@ -1438,6 +1489,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);
 
@@ -1454,9 +1506,13 @@ sub DeclarePtrVariables($$)
 {
        my ($self,$e) = @_;
        foreach my $l (@{$e->{LEVELS}}) {
+               my $size = 32;
                if ($l->{TYPE} eq "POINTER" and 
                        not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
-                       $self->pidl("uint32_t _ptr_$e->{NAME};");
+                       if ($l->{POINTER_TYPE} eq "relative_short") {
+                               $size = 16;
+                       }
+                       $self->pidl("uint${size}_t _ptr_$e->{NAME};");
                        last;
                }
        }
@@ -1475,6 +1531,24 @@ sub DeclareArrayVariables($$)
        }
 }
 
+sub DeclareArrayVariablesNoZero($$$)
+{
+       my ($self,$e,$env) = @_;
+
+       foreach my $l (@{$e->{LEVELS}}) {
+               next if has_fast_array($e,$l);
+               next if is_charset_array($e,$l);
+               if ($l->{TYPE} eq "ARRAY") {
+                   my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
+                   if ($length eq "0") {
+                       warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
+                   } else {
+                       $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
+                   }
+               }
+       }
+}
+
 sub DeclareMemCtxVariables($$)
 {
        my ($self,$e) = @_;
@@ -1505,6 +1579,8 @@ sub ParseStructPullPrimitives($$$$$)
        $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
 
        $self->add_deferred();
+
+       $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));");
 }
 
 sub ParseStructPullDeferred($$$$$)
@@ -1566,7 +1642,7 @@ sub ParseStructNdrSize($$$$)
        if (my $flags = has_property($t, "flag")) {
                $self->pidl("flags |= $flags;");
        }
-       $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
+       $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);");
 }
 
 sub DeclStruct($$$$)
@@ -1578,7 +1654,7 @@ sub DeclStruct($$$$)
 sub ArgsStructNdrSize($$$)
 {
        my ($d, $name, $varname) = @_;
-       return "const struct $name *$varname, struct smb_iconv_convenience *ic, int flags";
+       return "const struct $name *$varname, int flags";
 }
 
 $typefamily{STRUCT} = {
@@ -1601,7 +1677,7 @@ sub ParseUnionNdrSize($$$)
                $self->pidl("flags |= $flags;");
        }
 
-       $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name, ic);");
+       $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);");
 }
 
 sub ParseUnionPushPrimitives($$$$)
@@ -1610,12 +1686,20 @@ sub ParseUnionPushPrimitives($$$$)
 
        my $have_default = 0;
 
-       $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
+       $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
+
+       if (defined($e->{ALIGN})) {
+               $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+       }
 
        if (defined($e->{SWITCH_TYPE})) {
                $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
        }
 
+       if (defined($e->{ALIGN})) {
+               $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+       }
+
        $self->pidl("switch (level) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1641,7 +1725,7 @@ sub ParseUnionPushPrimitives($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1653,7 +1737,7 @@ sub ParseUnionPushDeferred($$$$)
 
        my $have_default = 0;
 
-       $self->pidl("int level = ndr_push_get_switch_value($ndr, $varname);");
+       $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
        if (defined($e->{PROPERTIES}{relative_base})) {
                # retrieve the current offset as base for relative pointers
                # based on the toplevel struct/union
@@ -1677,7 +1761,7 @@ sub ParseUnionPushDeferred($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1712,7 +1796,7 @@ sub ParseUnionPrint($$$$$)
        my ($self,$e,$ndr,$name,$varname) = @_;
        my $have_default = 0;
 
-       $self->pidl("int level;");
+       $self->pidl("uint32_t level;");
        foreach my $el (@{$e->{ELEMENTS}}) {
                $self->DeclareArrayVariables($el);
        }
@@ -1753,13 +1837,21 @@ sub ParseUnionPullPrimitives($$$$$)
        my ($self,$e,$ndr,$varname,$switch_type) = @_;
        my $have_default = 0;
 
+       if (defined($e->{ALIGN})) {
+               $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+       }
+
        if (defined($switch_type)) {
                $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
                $self->pidl("if (_level != level) {"); 
-               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname\", _level);");
+               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
                $self->pidl("}");
        }
 
+       if (defined($e->{ALIGN})) {
+               $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+       }
+
        $self->pidl("switch (level) {");
        $self->indent;
        foreach my $el (@{$e->{ELEMENTS}}) {
@@ -1786,7 +1878,7 @@ sub ParseUnionPullPrimitives($$$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1820,7 +1912,7 @@ sub ParseUnionPullDeferred($$$$)
        }
        if (! $have_default) {
                $self->pidl("default:");
-               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);");
+               $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
        }
        $self->deindent;
        $self->pidl("}");
@@ -1835,7 +1927,7 @@ sub ParseUnionPull($$$$)
        my ($self,$e,$ndr,$varname) = @_;
        my $switch_type = $e->{SWITCH_TYPE};
 
-       $self->pidl("int level;");
+       $self->pidl("uint32_t level;");
        if (defined($switch_type)) {
                if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
                        $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
@@ -1881,7 +1973,7 @@ sub DeclUnion($$$$)
 sub ArgsUnionNdrSize($$)
 {
        my ($d,$name) = @_;
-       return "const union $name *r, uint32_t level, struct smb_iconv_convenience *ic, int flags";
+       return "const union $name *r, uint32_t level, int flags";
 }
 
 $typefamily{UNION} = {
@@ -1979,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) {");
@@ -2095,7 +2188,7 @@ sub AllocateArrayLevel($$$$$$)
                $self->pidl("}");
                if (grep(/in/,@{$e->{DIRECTION}}) and
                    grep(/out/,@{$e->{DIRECTION}})) {
-                       $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, $size * sizeof(*r->in.$e->{NAME}));");
+                       $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
                }
                return;
        }
@@ -2243,7 +2336,6 @@ sub FunctionCallEntry($$)
        $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
        $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
        $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;
 }
@@ -2264,7 +2356,7 @@ sub FunctionTable($$)
        foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
                $count += $self->FunctionCallEntry($d);
        }
-       $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }");
+       $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }");
        $self->pidl("};");
        $self->pidl("");
 
@@ -2548,7 +2640,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>");