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
4949 static void Fp_toString(js_State *J)
5050 {
5151 js_Object *self = js_toobject(J, 0);
52− char *s;
53− int i, n;
52+ js_Buffer *sb = NULL;
53+ int i;
5454
5555 if (!js_iscallable(J, 0))
5656 js_typeerror(J, "not a function");
5757
5858 if (self->type == JS_CFUNCTION || self->type == JS_CSCRIPT) {
5959 js_Function *F = self->u.f.function;
60− n = strlen("function () { ... }");
61− n += strlen(F->name);
62− for (i = 0; i < F->numparams; ++i)
63− n += strlen(F->vartab[i]) + 1;
64− s = js_malloc(J, n + 1);
65− strcpy(s, "function ");
66− strcat(s, F->name);
67− strcat(s, "(");
60+
61+ if (js_try(J)) {
62+ js_free(J, sb);
63+ js_throw(J);
64+ }
65+
66+ js_puts(J, &sb, "function ");
67+ js_puts(J, &sb, F->name);
68+ js_putc(J, &sb, '(');
6869 for (i = 0; i < F->numparams; ++i) {
69− if (i > 0) strcat(s, ",");
70− strcat(s, F->vartab[i]);
70+ if (i > 0) js_putc(J, &sb, ',');
71+ js_puts(J, &sb, F->vartab[i]);
7172 }
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) {
7379 if (js_try(J)) {
74− js_free(J, s);
80+ js_free(J, sb);
7581 js_throw(J);
7682 }
77− js_pushstring(J, s);
78− js_free(J, s);
83+
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);
7989 js_endtry(J);
90+ js_free(J, sb);
8091 } else {
8192 js_pushliteral(J, "function () { ... }");
8293 }

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