From f94bff7935d031f3114980715381af226ae3ac75 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 27 Mar 2014 14:05:31 -0400 Subject: [PATCH] cmd/dist: zero output variables on entry to goc2c functions Zeroing the outputs makes sure that during function calls in those functions we do not let the garbage collector treat uninitialized values as pointers. The garbage collector may still see uninitialized values if a preemption occurs during the function prologue, before the zeroing has had a chance to run. This reduces the number of 'bad pointer' messages when that runtime check is enabled, but it doesn't fix all of them, so the check is still disabled. It will also avoid leaks, although I doubt any of these were particularly serious. LGTM=iant, khr R=iant, khr CC=golang-codereviews https://golang.org/cl/80850044 --- src/cmd/dist/goc2c.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/cmd/dist/goc2c.c b/src/cmd/dist/goc2c.c index 36b43f56eb..38627657ec 100644 --- a/src/cmd/dist/goc2c.c +++ b/src/cmd/dist/goc2c.c @@ -524,6 +524,7 @@ write_6g_func_header(char *package, char *name, struct params *params, int paramwid, struct params *rets) { int first, n; + struct params *p; bwritef(output, "void\n"); if(!contains(name, "·")) @@ -546,6 +547,24 @@ write_6g_func_header(char *package, char *name, struct params *params, write_params(rets, &first); bwritef(output, ")\n{\n"); + + for (p = rets; p != nil; p = p->next) { + if(streq(p->name, "...")) + continue; + if(streq(p->type, "Slice")) + bwritef(output, "\t%s.array = 0;\n\t%s.len = 0;\n\t%s.cap = 0;\n", p->name, p->name, p->name); + else if(streq(p->type, "String")) + bwritef(output, "\t%s.str = 0;\n\t%s.len = 0;\n", p->name, p->name); + else if(streq(p->type, "Eface")) + bwritef(output, "\t%s.type = 0;\n\t%s.data = 0;\n", p->name, p->name); + else if(streq(p->type, "Iface")) + bwritef(output, "\t%s.tab = 0;\n\t%s.data = 0;\n", p->name, p->name); + else if(streq(p->type, "Complex128")) + bwritef(output, "\t%s.real = 0;\n\t%s.imag = 0;\n", p->name, p->name); + else + bwritef(output, "\t%s = 0;\n", p->name); + bwritef(output, "\tFLUSH(&%s);\n", p->name); + } } /* Write a 6g function trailer. */ -- 2.50.0