History

Fix leak in Function.prototype.toString.

Tor Andersson committed 9 years ago

Commit 8c27b121 parent 991f5e71 file · +27 −16Browse files

Showing the changes to jsfunction.c (1 of 1 file). Show all

Unified · Split

jsfunction.c+27−16
@@ -49,34 +49,45 @@ static void jsB_Function_prototype(js_State *J) · 48 unmodified lines
49static void Fp_toString(js_State *J)49static void Fp_toString(js_State *J)
50{50{
51 js_Object *self = js_toobject(J, 0);51 js_Object *self = js_toobject(J, 0);
52 char *s;52 js_Buffer *sb = NULL;
53 int i, n;53 int i;
54 54
55 if (!js_iscallable(J, 0))55 if (!js_iscallable(J, 0))
56 js_typeerror(J, "not a function");56 js_typeerror(J, "not a function");
57 57
58 if (self->type == JS_CFUNCTION || self->type == JS_CSCRIPT) {58 if (self->type == JS_CFUNCTION || self->type == JS_CSCRIPT) {
59 js_Function *F = self->u.f.function;59 js_Function *F = self->u.f.function;
60 n = strlen("function () { ... }");60
61 n += strlen(F->name);61 if (js_try(J)) {
62 for (i = 0; i < F->numparams; ++i)62 js_free(J, sb);
63 n += strlen(F->vartab[i]) + 1;63 js_throw(J);
64 s = js_malloc(J, n + 1);64 }
65 strcpy(s, "function ");65
66 strcat(s, F->name);66 js_puts(J, &sb, "function ");
67 strcat(s, "(");67 js_puts(J, &sb, F->name);
68 js_putc(J, &sb, '(');
68 for (i = 0; i < F->numparams; ++i) {69 for (i = 0; i < F->numparams; ++i) {
69 if (i > 0) strcat(s, ",");70 if (i > 0) js_putc(J, &sb, ',');
70 strcat(s, F->vartab[i]);71 js_puts(J, &sb, F->vartab[i]);
71 }72 }
72 strcat(s, ") { ... }");73 js_puts(J, &sb, ") { ... }");
74
75 js_pushstring(J, sb->s);
76 js_endtry(J);
77 js_free(J, sb);
78 } else if (self->type == JS_CCFUNCTION) {
73 if (js_try(J)) {79 if (js_try(J)) {
74 js_free(J, s);80 js_free(J, sb);
75 js_throw(J);81 js_throw(J);
76 }82 }
77 js_pushstring(J, s);83
78 js_free(J, s);84 js_puts(J, &sb, "function ");
85 js_puts(J, &sb, self->u.c.name);
86 js_puts(J, &sb, "() { ... }");
87
88 js_pushstring(J, sb->s);
79 js_endtry(J);89 js_endtry(J);
90 js_free(J, sb);
80 } else {91 } else {
81 js_pushliteral(J, "function () { ... }");92 js_pushliteral(J, "function () { ... }");
82 }93 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t