rpcgen: add g_auto function support

This will eliminate the need to call xdr_free to clear
pointers from data structures.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2022-12-21 12:19:46 -05:00
parent a62486b95f
commit 086fa214bb
7 changed files with 298 additions and 62 deletions

View File

@ -1471,7 +1471,7 @@ exclude_file_name_regexp--sc_prohibit_mixed_case_abbreviations = \
^src/(vbox/vbox_CAPI.*.h|esx/esx_vi.(c|h)|esx/esx_storage_backend_iscsi.c)$$ ^src/(vbox/vbox_CAPI.*.h|esx/esx_vi.(c|h)|esx/esx_storage_backend_iscsi.c)$$
exclude_file_name_regexp--sc_prohibit_empty_first_line = \ exclude_file_name_regexp--sc_prohibit_empty_first_line = \
^tests/vmwareverdata/fusion-5.0.3.txt$$ ^tests/vmwareverdata/fusion-5.0.3.txt|scripts/rpcgen/tests/demo\.c$$
exclude_file_name_regexp--sc_prohibit_useless_translation = \ exclude_file_name_regexp--sc_prohibit_useless_translation = \
^tests/virpolkittest.c ^tests/virpolkittest.c

View File

@ -8,6 +8,7 @@ import sys
from rpcgen.parser import XDRParser from rpcgen.parser import XDRParser
from rpcgen.generator import ( from rpcgen.generator import (
XDRTypeDeclarationGenerator, XDRTypeDeclarationGenerator,
XDRTypeImplementationGenerator,
XDRMarshallDeclarationGenerator, XDRMarshallDeclarationGenerator,
XDRMarshallImplementationGenerator, XDRMarshallImplementationGenerator,
) )
@ -59,6 +60,7 @@ def main():
if args.mode == "header": if args.mode == "header":
print("/* This file is auto-generated from %s */\n" % args.input, file=outfp) print("/* This file is auto-generated from %s */\n" % args.input, file=outfp)
print("#include <rpc/rpc.h>", file=outfp) print("#include <rpc/rpc.h>", file=outfp)
print('#include "internal.h"', file=outfp)
for h in args.header: for h in args.header:
print('#include "%s"' % h, file=outfp) print('#include "%s"' % h, file=outfp)
print("", file=outfp) print("", file=outfp)
@ -73,6 +75,8 @@ def main():
for h in args.header: for h in args.header:
print('#include "%s"' % h, file=outfp) print('#include "%s"' % h, file=outfp)
print("", file=outfp) print("", file=outfp)
generator = XDRTypeImplementationGenerator(spec)
print(generator.visit(), file=outfp)
generator = XDRMarshallImplementationGenerator(spec) generator = XDRMarshallImplementationGenerator(spec)
print(generator.visit(), file=outfp) print(generator.visit(), file=outfp)
elif args.mode == "repr": elif args.mode == "repr":

View File

@ -30,24 +30,42 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
) + "%stypedef enum %s %s;\n" % (indent, obj.name, obj.name) ) + "%stypedef enum %s %s;\n" % (indent, obj.name, obj.name)
return code return code
def visit_definition_struct(self, obj, indent, context): def generate_cleanup(self, name, indent):
code = "%sstruct %s %s;\n" % ( code = "%svoid xdr_%s_clear(%s *objp);\n" % (
indent, indent,
obj.name, name,
self.visit_object(obj.body, indent), name,
) + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) ) + "%sG_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(%s, xdr_%s_clear);\n" % (
indent,
name,
name,
)
return code
def visit_definition_struct(self, obj, indent, context):
code = (
"%sstruct %s %s;\n"
% (indent, obj.name, self.visit_object(obj.body, indent))
+ "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name)
+ self.generate_cleanup(obj.name, indent)
)
return code return code
def visit_definition_union(self, obj, indent, context): def visit_definition_union(self, obj, indent, context):
code = "%sstruct %s %s;\n" % ( code = (
indent, "%sstruct %s %s;\n"
obj.name, % (indent, obj.name, self.visit_object(obj.body, indent, obj.name))
self.visit_object(obj.body, indent, obj.name), + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name)
) + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) + self.generate_cleanup(obj.name, indent)
)
return code return code
def visit_definition_typedef(self, obj, indent, context): def visit_definition_typedef(self, obj, indent, context):
return "%stypedef %s;\n" % (indent, self.visit_object(obj.decl, indent)) code = "%stypedef %s;\n" % (
indent,
self.visit_object(obj.decl, indent),
) + self.generate_cleanup(obj.decl.identifier, indent)
return code
def visit_declaration_scalar(self, obj, indent, context): def visit_declaration_scalar(self, obj, indent, context):
return "%s %s" % (self.visit_object(obj.typ, indent), obj.identifier) return "%s %s" % (self.visit_object(obj.typ, indent), obj.identifier)
@ -169,6 +187,30 @@ class XDRTypeDeclarationGenerator(XDRVisitor):
return code return code
class XDRTypeImplementationGenerator(XDRVisitor):
def visit_definition_enum(self, obj, indent, context):
pass
def generate_cleanup(self, name, indent):
code = (
"\n"
+ "%svoid xdr_%s_clear(%s *objp)\n" % (indent, name, name)
+ "%s{\n" % indent
+ "%s xdr_free((xdrproc_t)xdr_%s, (char *)objp);\n" % (indent, name)
+ "%s}\n" % indent
)
return code
def visit_definition_union(self, obj, indent, context):
return self.generate_cleanup(obj.name, indent)
def visit_definition_struct(self, obj, indent, context):
return self.generate_cleanup(obj.name, indent)
def visit_definition_typedef(self, obj, indent, context):
return self.generate_cleanup(obj.decl.identifier, indent)
class XDRMarshallDeclarationGenerator(XDRVisitor): class XDRMarshallDeclarationGenerator(XDRVisitor):
def visit_definition_enum(self, obj, indent, context): def visit_definition_enum(self, obj, indent, context):
return "%sextern bool_t xdr_%s(XDR *, %s*);\n" % (indent, obj.name, obj.name) return "%sextern bool_t xdr_%s(XDR *, %s*);\n" % (indent, obj.name, obj.name)

View File

@ -1,3 +1,147 @@
void xdr_TestStruct_clear(TestStruct *objp)
{
xdr_free((xdrproc_t)xdr_TestStruct, (char *)objp);
}
void xdr_TestUnion_clear(TestUnion *objp)
{
xdr_free((xdrproc_t)xdr_TestUnion, (char *)objp);
}
void xdr_TestUnionVoidDefault_clear(TestUnionVoidDefault *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionVoidDefault, (char *)objp);
}
void xdr_TestUnionNoDefault_clear(TestUnionNoDefault *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionNoDefault, (char *)objp);
}
void xdr_TestIntScalar_clear(TestIntScalar *objp)
{
xdr_free((xdrproc_t)xdr_TestIntScalar, (char *)objp);
}
void xdr_TestIntPointer_clear(TestIntPointer *objp)
{
xdr_free((xdrproc_t)xdr_TestIntPointer, (char *)objp);
}
void xdr_TestIntFixedArray_clear(TestIntFixedArray *objp)
{
xdr_free((xdrproc_t)xdr_TestIntFixedArray, (char *)objp);
}
void xdr_TestIntVariableArray_clear(TestIntVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestIntVariableArray, (char *)objp);
}
void xdr_TestStringVariableArray_clear(TestStringVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestStringVariableArray, (char *)objp);
}
void xdr_TestOpaqueFixedArray_clear(TestOpaqueFixedArray *objp)
{
xdr_free((xdrproc_t)xdr_TestOpaqueFixedArray, (char *)objp);
}
void xdr_TestOpaqueVariableArray_clear(TestOpaqueVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestOpaqueVariableArray, (char *)objp);
}
void xdr_TestEnumScalar_clear(TestEnumScalar *objp)
{
xdr_free((xdrproc_t)xdr_TestEnumScalar, (char *)objp);
}
void xdr_TestEnumPointer_clear(TestEnumPointer *objp)
{
xdr_free((xdrproc_t)xdr_TestEnumPointer, (char *)objp);
}
void xdr_TestEnumFixedArray_clear(TestEnumFixedArray *objp)
{
xdr_free((xdrproc_t)xdr_TestEnumFixedArray, (char *)objp);
}
void xdr_TestEnumVariableArray_clear(TestEnumVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestEnumVariableArray, (char *)objp);
}
void xdr_TestStructScalar_clear(TestStructScalar *objp)
{
xdr_free((xdrproc_t)xdr_TestStructScalar, (char *)objp);
}
void xdr_TestStructPointer_clear(TestStructPointer *objp)
{
xdr_free((xdrproc_t)xdr_TestStructPointer, (char *)objp);
}
void xdr_TestStructFixedArray_clear(TestStructFixedArray *objp)
{
xdr_free((xdrproc_t)xdr_TestStructFixedArray, (char *)objp);
}
void xdr_TestStructVariableArray_clear(TestStructVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestStructVariableArray, (char *)objp);
}
void xdr_TestUnionScalar_clear(TestUnionScalar *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionScalar, (char *)objp);
}
void xdr_TestUnionPointer_clear(TestUnionPointer *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionPointer, (char *)objp);
}
void xdr_TestUnionFixedArray_clear(TestUnionFixedArray *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionFixedArray, (char *)objp);
}
void xdr_TestUnionVariableArray_clear(TestUnionVariableArray *objp)
{
xdr_free((xdrproc_t)xdr_TestUnionVariableArray, (char *)objp);
}
void xdr_TestStructAllTypes_clear(TestStructAllTypes *objp)
{
xdr_free((xdrproc_t)xdr_TestStructAllTypes, (char *)objp);
}
bool_t bool_t
xdr_TestEnum(XDR *xdrs, TestEnum *objp) xdr_TestEnum(XDR *xdrs, TestEnum *objp)
{ {

View File

@ -9,6 +9,8 @@ struct TestStruct {
char c2; char c2;
}; };
typedef struct TestStruct TestStruct; typedef struct TestStruct TestStruct;
void xdr_TestStruct_clear(TestStruct *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStruct, xdr_TestStruct_clear);
struct TestUnion { struct TestUnion {
int type; int type;
@ -19,6 +21,8 @@ struct TestUnion {
} TestUnion_u; } TestUnion_u;
}; };
typedef struct TestUnion TestUnion; typedef struct TestUnion TestUnion;
void xdr_TestUnion_clear(TestUnion *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnion, xdr_TestUnion_clear);
struct TestUnionVoidDefault { struct TestUnionVoidDefault {
int type; int type;
@ -28,6 +32,8 @@ struct TestUnionVoidDefault {
} TestUnionVoidDefault_u; } TestUnionVoidDefault_u;
}; };
typedef struct TestUnionVoidDefault TestUnionVoidDefault; typedef struct TestUnionVoidDefault TestUnionVoidDefault;
void xdr_TestUnionVoidDefault_clear(TestUnionVoidDefault *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionVoidDefault, xdr_TestUnionVoidDefault_clear);
struct TestUnionNoDefault { struct TestUnionNoDefault {
int type; int type;
@ -37,59 +43,99 @@ struct TestUnionNoDefault {
} TestUnionNoDefault_u; } TestUnionNoDefault_u;
}; };
typedef struct TestUnionNoDefault TestUnionNoDefault; typedef struct TestUnionNoDefault TestUnionNoDefault;
void xdr_TestUnionNoDefault_clear(TestUnionNoDefault *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionNoDefault, xdr_TestUnionNoDefault_clear);
typedef int TestIntScalar; typedef int TestIntScalar;
void xdr_TestIntScalar_clear(TestIntScalar *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntScalar, xdr_TestIntScalar_clear);
typedef int *TestIntPointer; typedef int *TestIntPointer;
void xdr_TestIntPointer_clear(TestIntPointer *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntPointer, xdr_TestIntPointer_clear);
typedef int TestIntFixedArray[3]; typedef int TestIntFixedArray[3];
void xdr_TestIntFixedArray_clear(TestIntFixedArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntFixedArray, xdr_TestIntFixedArray_clear);
typedef struct { typedef struct {
u_int TestIntVariableArray_len; u_int TestIntVariableArray_len;
int *TestIntVariableArray_val; int *TestIntVariableArray_val;
} TestIntVariableArray; } TestIntVariableArray;
void xdr_TestIntVariableArray_clear(TestIntVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntVariableArray, xdr_TestIntVariableArray_clear);
typedef char *TestStringVariableArray; typedef char *TestStringVariableArray;
void xdr_TestStringVariableArray_clear(TestStringVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStringVariableArray, xdr_TestStringVariableArray_clear);
typedef char TestOpaqueFixedArray[9]; typedef char TestOpaqueFixedArray[9];
void xdr_TestOpaqueFixedArray_clear(TestOpaqueFixedArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestOpaqueFixedArray, xdr_TestOpaqueFixedArray_clear);
typedef struct { typedef struct {
u_int TestOpaqueVariableArray_len; u_int TestOpaqueVariableArray_len;
char *TestOpaqueVariableArray_val; char *TestOpaqueVariableArray_val;
} TestOpaqueVariableArray; } TestOpaqueVariableArray;
void xdr_TestOpaqueVariableArray_clear(TestOpaqueVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestOpaqueVariableArray, xdr_TestOpaqueVariableArray_clear);
typedef TestEnum TestEnumScalar; typedef TestEnum TestEnumScalar;
void xdr_TestEnumScalar_clear(TestEnumScalar *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumScalar, xdr_TestEnumScalar_clear);
typedef TestEnum *TestEnumPointer; typedef TestEnum *TestEnumPointer;
void xdr_TestEnumPointer_clear(TestEnumPointer *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumPointer, xdr_TestEnumPointer_clear);
typedef TestEnum TestEnumFixedArray[13]; typedef TestEnum TestEnumFixedArray[13];
void xdr_TestEnumFixedArray_clear(TestEnumFixedArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumFixedArray, xdr_TestEnumFixedArray_clear);
typedef struct { typedef struct {
u_int TestEnumVariableArray_len; u_int TestEnumVariableArray_len;
TestEnum *TestEnumVariableArray_val; TestEnum *TestEnumVariableArray_val;
} TestEnumVariableArray; } TestEnumVariableArray;
void xdr_TestEnumVariableArray_clear(TestEnumVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumVariableArray, xdr_TestEnumVariableArray_clear);
typedef TestStruct TestStructScalar; typedef TestStruct TestStructScalar;
void xdr_TestStructScalar_clear(TestStructScalar *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructScalar, xdr_TestStructScalar_clear);
typedef TestStruct *TestStructPointer; typedef TestStruct *TestStructPointer;
void xdr_TestStructPointer_clear(TestStructPointer *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructPointer, xdr_TestStructPointer_clear);
typedef TestStruct TestStructFixedArray[17]; typedef TestStruct TestStructFixedArray[17];
void xdr_TestStructFixedArray_clear(TestStructFixedArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructFixedArray, xdr_TestStructFixedArray_clear);
typedef struct { typedef struct {
u_int TestStructVariableArray_len; u_int TestStructVariableArray_len;
TestStruct *TestStructVariableArray_val; TestStruct *TestStructVariableArray_val;
} TestStructVariableArray; } TestStructVariableArray;
void xdr_TestStructVariableArray_clear(TestStructVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructVariableArray, xdr_TestStructVariableArray_clear);
typedef TestUnion TestUnionScalar; typedef TestUnion TestUnionScalar;
void xdr_TestUnionScalar_clear(TestUnionScalar *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionScalar, xdr_TestUnionScalar_clear);
typedef TestUnion *TestUnionPointer; typedef TestUnion *TestUnionPointer;
void xdr_TestUnionPointer_clear(TestUnionPointer *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionPointer, xdr_TestUnionPointer_clear);
typedef TestUnion TestUnionFixedArray[21]; typedef TestUnion TestUnionFixedArray[21];
void xdr_TestUnionFixedArray_clear(TestUnionFixedArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionFixedArray, xdr_TestUnionFixedArray_clear);
typedef struct { typedef struct {
u_int TestUnionVariableArray_len; u_int TestUnionVariableArray_len;
TestUnion *TestUnionVariableArray_val; TestUnion *TestUnionVariableArray_val;
} TestUnionVariableArray; } TestUnionVariableArray;
void xdr_TestUnionVariableArray_clear(TestUnionVariableArray *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionVariableArray, xdr_TestUnionVariableArray_clear);
#define TestConstDec 25 #define TestConstDec 25
@ -164,6 +210,8 @@ struct TestStructAllTypes {
TestUnionVariableArray tuva; TestUnionVariableArray tuva;
}; };
typedef struct TestStructAllTypes TestStructAllTypes; typedef struct TestStructAllTypes TestStructAllTypes;
void xdr_TestStructAllTypes_clear(TestStructAllTypes *objp);
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructAllTypes, xdr_TestStructAllTypes_clear);
extern bool_t xdr_TestEnum(XDR *, TestEnum*); extern bool_t xdr_TestEnum(XDR *, TestEnum*);

View File

@ -73,13 +73,6 @@ static void test_xdr(xdrproc_t proc, void *vorig, void *vnew, const char *testna
g_assert_cmpint(memcmp(buf, expected, actlen), ==, 0); g_assert_cmpint(memcmp(buf, expected, actlen), ==, 0);
xdr_destroy(&xdr); xdr_destroy(&xdr);
/* Step 4: free mem from the new object only; the orig
* was on the stack so leave untouched */
xdrmem_create(&xdr, buf, buflen, XDR_FREE);
ret = !!proc(&xdr, vnew);
g_assert_cmpint(ret, ==, true);
cleanup: cleanup:
xdr_destroy(&xdr); xdr_destroy(&xdr);
} }
@ -97,7 +90,7 @@ static void test_struct(void)
TestStruct vorig = { TestStruct vorig = {
.c1 = 'a', .c2 = 'b', .c1 = 'a', .c2 = 'b',
}; };
TestStruct vnew = {0}; g_auto(TestStruct) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStruct, &vorig, &vnew, "struct", false); test_xdr((xdrproc_t)xdr_TestStruct, &vorig, &vnew, "struct", false);
} }
@ -107,7 +100,7 @@ static void test_union_case(void)
TestUnion vorig = { TestUnion vorig = {
.type = 20, .TestUnion_u = { .i1 = 1729 }, .type = 20, .TestUnion_u = { .i1 = 1729 },
}; };
TestUnion vnew = {0}; g_auto(TestUnion) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_case", false); test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_case", false);
} }
@ -117,7 +110,7 @@ static void test_union_default(void)
TestUnion vorig = { TestUnion vorig = {
.type = 87539319, .TestUnion_u = { .i3 = 1729 }, .type = 87539319, .TestUnion_u = { .i3 = 1729 },
}; };
TestUnion vnew = {0}; g_auto(TestUnion) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_default", false); test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_default", false);
} }
@ -127,7 +120,7 @@ static void test_union_void_default_case(void)
TestUnionVoidDefault vorig = { TestUnionVoidDefault vorig = {
.type = 21, .TestUnionVoidDefault_u = { .i1 = 1729 }, .type = 21, .TestUnionVoidDefault_u = { .i1 = 1729 },
}; };
TestUnionVoidDefault vnew = {0}; g_auto(TestUnionVoidDefault) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_case", false); test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_case", false);
} }
@ -137,7 +130,7 @@ static void test_union_void_default_default(void)
TestUnionVoidDefault vorig = { TestUnionVoidDefault vorig = {
.type = 87539319 .type = 87539319
}; };
TestUnionVoidDefault vnew = {0}; g_auto(TestUnionVoidDefault) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_default", false); test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_default", false);
} }
@ -147,7 +140,7 @@ static void test_union_no_default_case(void)
TestUnionNoDefault vorig = { TestUnionNoDefault vorig = {
.type = 22, .TestUnionNoDefault_u = { .i1 = 1729 }, .type = 22, .TestUnionNoDefault_u = { .i1 = 1729 },
}; };
TestUnionNoDefault vnew = {0}; g_auto(TestUnionNoDefault) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_case", false); test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_case", false);
} }
@ -157,7 +150,7 @@ static void test_union_no_default_default(void)
TestUnionNoDefault vorig = { TestUnionNoDefault vorig = {
.type = 87539319, .type = 87539319,
}; };
TestUnionNoDefault vnew = {0}; g_auto(TestUnionNoDefault) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_default", true); test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_default", true);
} }
@ -165,7 +158,7 @@ static void test_union_no_default_default(void)
static void test_int_scalar(void) static void test_int_scalar(void)
{ {
TestIntScalar vorig = 1729; TestIntScalar vorig = 1729;
TestIntScalar vnew = 0; g_auto(TestIntScalar) vnew = 0;
test_xdr((xdrproc_t)xdr_TestIntScalar, &vorig, &vnew, "int_scalar", false); test_xdr((xdrproc_t)xdr_TestIntScalar, &vorig, &vnew, "int_scalar", false);
} }
@ -174,7 +167,7 @@ static void test_int_pointer_set(void)
{ {
int vorigp = 1729; int vorigp = 1729;
TestIntPointer vorig = &vorigp; TestIntPointer vorig = &vorigp;
TestIntPointer vnew = NULL; g_auto(TestIntPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_set", false); test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_set", false);
} }
@ -182,7 +175,7 @@ static void test_int_pointer_set(void)
static void test_int_pointer_null(void) static void test_int_pointer_null(void)
{ {
TestIntPointer vorig = NULL; TestIntPointer vorig = NULL;
TestIntPointer vnew = NULL; g_auto(TestIntPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_null", false); test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_null", false);
} }
@ -190,7 +183,7 @@ static void test_int_pointer_null(void)
static void test_int_fixed_array(void) static void test_int_fixed_array(void)
{ {
TestIntFixedArray vorig = { 1729, 0, 87539319 }; TestIntFixedArray vorig = { 1729, 0, 87539319 };
TestIntFixedArray vnew = {0}; g_auto(TestIntFixedArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestIntFixedArray, test_xdr((xdrproc_t)xdr_TestIntFixedArray,
vorig, vnew, "int_fixed_array", false); vorig, vnew, "int_fixed_array", false);
@ -202,7 +195,7 @@ static void test_int_variable_array_set(void)
.TestIntVariableArray_len = 3, .TestIntVariableArray_len = 3,
.TestIntVariableArray_val = (int[]) { 1729, 0, 87539319 } .TestIntVariableArray_val = (int[]) { 1729, 0, 87539319 }
}; };
TestIntVariableArray vnew = {0}; g_auto(TestIntVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestIntVariableArray, test_xdr((xdrproc_t)xdr_TestIntVariableArray,
&vorig, &vnew, "int_variable_array_set", false); &vorig, &vnew, "int_variable_array_set", false);
@ -214,7 +207,7 @@ static void test_int_variable_array_overflow(void)
.TestIntVariableArray_len = 6, .TestIntVariableArray_len = 6,
.TestIntVariableArray_val = (int[]) { 1729, 0, 87539319, 0, 1729 } .TestIntVariableArray_val = (int[]) { 1729, 0, 87539319, 0, 1729 }
}; };
TestIntVariableArray vnew = {0}; g_auto(TestIntVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestIntVariableArray, test_xdr((xdrproc_t)xdr_TestIntVariableArray,
&vorig, &vnew, "int_variable_array_overflow", true); &vorig, &vnew, "int_variable_array_overflow", true);
@ -226,7 +219,7 @@ static void test_int_variable_array_empty(void)
.TestIntVariableArray_len = 0, .TestIntVariableArray_len = 0,
.TestIntVariableArray_val = (int[]) {0}, .TestIntVariableArray_val = (int[]) {0},
}; };
TestIntVariableArray vnew = {0}; g_auto(TestIntVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestIntVariableArray, test_xdr((xdrproc_t)xdr_TestIntVariableArray,
&vorig, &vnew, "int_variable_array_empty", false); &vorig, &vnew, "int_variable_array_empty", false);
@ -235,7 +228,7 @@ static void test_int_variable_array_empty(void)
static void test_string_variable_array_set(void) static void test_string_variable_array_set(void)
{ {
TestStringVariableArray vorig = (TestStringVariableArray) "taxis"; TestStringVariableArray vorig = (TestStringVariableArray) "taxis";
TestStringVariableArray vnew = NULL; g_auto(TestStringVariableArray) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestStringVariableArray, test_xdr((xdrproc_t)xdr_TestStringVariableArray,
&vorig, &vnew, "string_variable_array_set", false); &vorig, &vnew, "string_variable_array_set", false);
@ -244,7 +237,7 @@ static void test_string_variable_array_set(void)
static void test_string_variable_array_empty(void) static void test_string_variable_array_empty(void)
{ {
TestStringVariableArray vorig = (TestStringVariableArray)""; TestStringVariableArray vorig = (TestStringVariableArray)"";
TestStringVariableArray vnew = NULL; g_auto(TestStringVariableArray) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestStringVariableArray, test_xdr((xdrproc_t)xdr_TestStringVariableArray,
&vorig, &vnew, "string_variable_array_empty", false); &vorig, &vnew, "string_variable_array_empty", false);
@ -253,7 +246,7 @@ static void test_string_variable_array_empty(void)
static void test_opaque_fixed_array(void) static void test_opaque_fixed_array(void)
{ {
TestOpaqueFixedArray vorig = { 0xca, 0xfe, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 }; TestOpaqueFixedArray vorig = { 0xca, 0xfe, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
TestOpaqueFixedArray vnew = {0}; g_auto(TestOpaqueFixedArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestOpaqueFixedArray, vorig, vnew, "opaque_fixed_array", false); test_xdr((xdrproc_t)xdr_TestOpaqueFixedArray, vorig, vnew, "opaque_fixed_array", false);
} }
@ -264,7 +257,7 @@ static void test_opaque_variable_array_set(void)
.TestOpaqueVariableArray_len = 3, .TestOpaqueVariableArray_len = 3,
.TestOpaqueVariableArray_val = (char[]) { 0xca, 0xfe, 0x12 }, .TestOpaqueVariableArray_val = (char[]) { 0xca, 0xfe, 0x12 },
}; };
TestOpaqueVariableArray vnew = {0}; g_auto(TestOpaqueVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray,
&vorig, &vnew, "opaque_variable_array_set", false); &vorig, &vnew, "opaque_variable_array_set", false);
@ -279,7 +272,7 @@ static void test_opaque_variable_array_overflow(void)
0xca, 0xfe, 0x12, 0xca, 0xfe, 0x12, 0xca, 0xfe, 0x12, 0xca, 0xfe, 0x12,
}, },
}; };
TestOpaqueVariableArray vnew = {0}; g_auto(TestOpaqueVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray,
&vorig, &vnew, "opaque_variable_array_overflow", true); &vorig, &vnew, "opaque_variable_array_overflow", true);
@ -291,7 +284,7 @@ static void test_opaque_variable_array_empty(void)
.TestOpaqueVariableArray_len = 0, .TestOpaqueVariableArray_len = 0,
.TestOpaqueVariableArray_val = (char[]) {0}, .TestOpaqueVariableArray_val = (char[]) {0},
}; };
TestOpaqueVariableArray vnew = {0}; g_auto(TestOpaqueVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray,
&vorig, &vnew, "opaque_variable_array_empty", false); &vorig, &vnew, "opaque_variable_array_empty", false);
@ -300,7 +293,7 @@ static void test_opaque_variable_array_empty(void)
static void test_enum_scalar(void) static void test_enum_scalar(void)
{ {
TestEnumScalar vorig = TEST_ENUM_TWO; TestEnumScalar vorig = TEST_ENUM_TWO;
TestEnumScalar vnew = 0; g_auto(TestEnumScalar) vnew = 0;
test_xdr((xdrproc_t)xdr_TestEnumScalar, test_xdr((xdrproc_t)xdr_TestEnumScalar,
&vorig, &vnew, "enum_scalar", false); &vorig, &vnew, "enum_scalar", false);
@ -310,7 +303,7 @@ static void test_enum_pointer_set(void)
{ {
TestEnum vorigp = TEST_ENUM_TWO; TestEnum vorigp = TEST_ENUM_TWO;
TestEnumPointer vorig = &vorigp; TestEnumPointer vorig = &vorigp;
TestEnumPointer vnew = NULL; g_auto(TestEnumPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestEnumPointer, test_xdr((xdrproc_t)xdr_TestEnumPointer,
&vorig, &vnew, "enum_pointer_set", false); &vorig, &vnew, "enum_pointer_set", false);
@ -319,7 +312,7 @@ static void test_enum_pointer_set(void)
static void test_enum_pointer_null(void) static void test_enum_pointer_null(void)
{ {
TestEnumPointer vorig = NULL; TestEnumPointer vorig = NULL;
TestEnumPointer vnew = NULL; g_auto(TestEnumPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestEnumPointer, test_xdr((xdrproc_t)xdr_TestEnumPointer,
&vorig, &vnew, "enum_pointer_null", false); &vorig, &vnew, "enum_pointer_null", false);
@ -332,7 +325,7 @@ static void test_enum_fixed_array(void)
TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO,
TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE
}; };
TestEnumFixedArray vnew = {0}; g_auto(TestEnumFixedArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestEnumFixedArray, vorig, vnew, "enum_fixed_array", false); test_xdr((xdrproc_t)xdr_TestEnumFixedArray, vorig, vnew, "enum_fixed_array", false);
} }
@ -345,7 +338,7 @@ static void test_enum_variable_array_set(void)
TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE,
}, },
}; };
TestEnumVariableArray vnew = {0}; g_auto(TestEnumVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestEnumVariableArray, test_xdr((xdrproc_t)xdr_TestEnumVariableArray,
&vorig, &vnew, "enum_variable_array_set", false); &vorig, &vnew, "enum_variable_array_set", false);
@ -362,7 +355,7 @@ static void test_enum_variable_array_overflow(void)
TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO,
} }
}; };
TestEnumVariableArray vnew = {0}; g_auto(TestEnumVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestEnumVariableArray, test_xdr((xdrproc_t)xdr_TestEnumVariableArray,
&vorig, &vnew, "enum_variable_array_overflow", true); &vorig, &vnew, "enum_variable_array_overflow", true);
@ -374,7 +367,7 @@ static void test_enum_variable_array_empty(void)
.TestEnumVariableArray_len = 0, .TestEnumVariableArray_len = 0,
.TestEnumVariableArray_val = (TestEnum[]) {0}, .TestEnumVariableArray_val = (TestEnum[]) {0},
}; };
TestEnumVariableArray vnew = {0}; g_auto(TestEnumVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestEnumVariableArray, test_xdr((xdrproc_t)xdr_TestEnumVariableArray,
&vorig, &vnew, "enum_variable_array_empty", false); &vorig, &vnew, "enum_variable_array_empty", false);
@ -386,7 +379,7 @@ static void test_enum_variable_array_empty(void)
static void test_struct_scalar(void) static void test_struct_scalar(void)
{ {
TestStructScalar vorig = TEST_STRUCT_INIT; TestStructScalar vorig = TEST_STRUCT_INIT;
TestStructScalar vnew = {0}; g_auto(TestStructScalar) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructScalar, test_xdr((xdrproc_t)xdr_TestStructScalar,
&vorig, &vnew, "struct_scalar", false); &vorig, &vnew, "struct_scalar", false);
@ -396,7 +389,7 @@ static void test_struct_pointer_set(void)
{ {
TestStruct vorigp = TEST_STRUCT_INIT; TestStruct vorigp = TEST_STRUCT_INIT;
TestStructPointer vorig = &vorigp; TestStructPointer vorig = &vorigp;
TestStructPointer vnew = NULL; g_auto(TestStructPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestStructPointer, test_xdr((xdrproc_t)xdr_TestStructPointer,
&vorig, &vnew, "struct_pointer_set", false); &vorig, &vnew, "struct_pointer_set", false);
@ -405,7 +398,7 @@ static void test_struct_pointer_set(void)
static void test_struct_pointer_null(void) static void test_struct_pointer_null(void)
{ {
TestStructPointer vorig = NULL; TestStructPointer vorig = NULL;
TestStructPointer vnew = NULL; g_auto(TestStructPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestStructPointer, test_xdr((xdrproc_t)xdr_TestStructPointer,
&vorig, &vnew, "struct_pointer_null", false); &vorig, &vnew, "struct_pointer_null", false);
@ -420,7 +413,7 @@ static void test_struct_fixed_array(void)
TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT, TEST_STRUCT_INIT,
TEST_STRUCT_INIT_ALT TEST_STRUCT_INIT_ALT
}; };
TestStructFixedArray vnew = {0}; g_auto(TestStructFixedArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructFixedArray, vorig, vnew, "struct_fixed_array", false); test_xdr((xdrproc_t)xdr_TestStructFixedArray, vorig, vnew, "struct_fixed_array", false);
} }
@ -433,7 +426,7 @@ static void test_struct_variable_array_set(void)
TEST_STRUCT_INIT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT,
}, },
}; };
TestStructVariableArray vnew = {0}; g_auto(TestStructVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructVariableArray, test_xdr((xdrproc_t)xdr_TestStructVariableArray,
&vorig, &vnew, "struct_variable_array_set", false); &vorig, &vnew, "struct_variable_array_set", false);
@ -451,7 +444,7 @@ static void test_struct_variable_array_overflow(void)
TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT,
} }
}; };
TestStructVariableArray vnew = {0}; g_auto(TestStructVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructVariableArray, test_xdr((xdrproc_t)xdr_TestStructVariableArray,
&vorig, &vnew, "struct_variable_array_overflow", true); &vorig, &vnew, "struct_variable_array_overflow", true);
@ -463,7 +456,7 @@ static void test_struct_variable_array_empty(void)
.TestStructVariableArray_len = 0, .TestStructVariableArray_len = 0,
.TestStructVariableArray_val = (TestStruct[]) {}, .TestStructVariableArray_val = (TestStruct[]) {},
}; };
TestStructVariableArray vnew = {0}; g_auto(TestStructVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructVariableArray, test_xdr((xdrproc_t)xdr_TestStructVariableArray,
&vorig, &vnew, "struct_variable_array_empty", false); &vorig, &vnew, "struct_variable_array_empty", false);
@ -475,7 +468,7 @@ static void test_struct_variable_array_empty(void)
static void test_union_scalar(void) static void test_union_scalar(void)
{ {
TestUnionScalar vorig = TEST_UNION_INIT; TestUnionScalar vorig = TEST_UNION_INIT;
TestUnionScalar vnew = {0}; g_auto(TestUnionScalar) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionScalar, test_xdr((xdrproc_t)xdr_TestUnionScalar,
&vorig, &vnew, "union_scalar", false); &vorig, &vnew, "union_scalar", false);
@ -485,7 +478,7 @@ static void test_union_pointer_set(void)
{ {
TestUnion vorigp = TEST_UNION_INIT; TestUnion vorigp = TEST_UNION_INIT;
TestUnionPointer vorig = &vorigp; TestUnionPointer vorig = &vorigp;
TestUnionPointer vnew = NULL; g_auto(TestUnionPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestUnionPointer, test_xdr((xdrproc_t)xdr_TestUnionPointer,
&vorig, &vnew, "union_pointer_set", false); &vorig, &vnew, "union_pointer_set", false);
@ -494,7 +487,7 @@ static void test_union_pointer_set(void)
static void test_union_pointer_null(void) static void test_union_pointer_null(void)
{ {
TestUnionPointer vorig = NULL; TestUnionPointer vorig = NULL;
TestUnionPointer vnew = NULL; g_auto(TestUnionPointer) vnew = NULL;
test_xdr((xdrproc_t)xdr_TestUnionPointer, test_xdr((xdrproc_t)xdr_TestUnionPointer,
&vorig, &vnew, "union_pointer_null", false); &vorig, &vnew, "union_pointer_null", false);
@ -509,7 +502,7 @@ static void test_union_fixed_array(void)
TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT, TEST_UNION_INIT, TEST_UNION_INIT,
TEST_UNION_INIT_ALT TEST_UNION_INIT_ALT
}; };
TestUnionFixedArray vnew = {0}; g_auto(TestUnionFixedArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionFixedArray, vorig, vnew, "union_fixed_array", false); test_xdr((xdrproc_t)xdr_TestUnionFixedArray, vorig, vnew, "union_fixed_array", false);
} }
@ -522,7 +515,7 @@ static void test_union_variable_array_set(void)
TEST_UNION_INIT, TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT, TEST_UNION_INIT, TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT,
}, },
}; };
TestUnionVariableArray vnew = {0}; g_auto(TestUnionVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionVariableArray, test_xdr((xdrproc_t)xdr_TestUnionVariableArray,
&vorig, &vnew, "union_variable_array_set", false); &vorig, &vnew, "union_variable_array_set", false);
@ -541,7 +534,7 @@ static void test_union_variable_array_overflow(void)
TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT,
} }
}; };
TestUnionVariableArray vnew = {0}; g_auto(TestUnionVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionVariableArray, test_xdr((xdrproc_t)xdr_TestUnionVariableArray,
&vorig, &vnew, "union_variable_array_overflow", true); &vorig, &vnew, "union_variable_array_overflow", true);
@ -553,7 +546,7 @@ static void test_union_variable_array_empty(void)
.TestUnionVariableArray_len = 0, .TestUnionVariableArray_len = 0,
.TestUnionVariableArray_val = (TestUnion[]) {}, .TestUnionVariableArray_val = (TestUnion[]) {},
}; };
TestUnionVariableArray vnew = {0}; g_auto(TestUnionVariableArray) vnew = {0};
test_xdr((xdrproc_t)xdr_TestUnionVariableArray, test_xdr((xdrproc_t)xdr_TestUnionVariableArray,
&vorig, &vnew, "union_variable_array_empty", false); &vorig, &vnew, "union_variable_array_empty", false);
@ -721,7 +714,7 @@ static void test_struct_all_types(void)
}, },
}, },
}; };
TestStructAllTypes vnew = {0}; g_auto(TestStructAllTypes) vnew = {0};
test_xdr((xdrproc_t)xdr_TestStructAllTypes, test_xdr((xdrproc_t)xdr_TestStructAllTypes,
&vorig, &vnew, "test_struct_all_types", false); &vorig, &vnew, "test_struct_all_types", false);

View File

@ -6,6 +6,7 @@ from pathlib import Path
from rpcgen.parser import XDRParser from rpcgen.parser import XDRParser
from rpcgen.generator import ( from rpcgen.generator import (
XDRTypeDeclarationGenerator, XDRTypeDeclarationGenerator,
XDRTypeImplementationGenerator,
XDRMarshallDeclarationGenerator, XDRMarshallDeclarationGenerator,
XDRMarshallImplementationGenerator, XDRMarshallImplementationGenerator,
) )
@ -42,7 +43,11 @@ def test_generate_source():
parser = XDRParser(fp) parser = XDRParser(fp)
spec = parser.parse() spec = parser.parse()
got = XDRMarshallImplementationGenerator(spec).visit() got = (
XDRTypeImplementationGenerator(spec).visit()
+ "\n"
+ XDRMarshallImplementationGenerator(spec).visit()
)
with h.open("r") as fp: with h.open("r") as fp:
want = fp.read() want = fp.read()