ref: ae54eaa65289a39b74009b911fe8542a1a0a62ca
parent: a7aae8f84b13e63661a445b50e89703e315e79d1
author: Tor Andersson <tor.andersson@artifex.com>
date: Wed Jul 5 08:40:56 EDT 2017
Move js_Buffer to jsi.h and fix some potential leaks.
--- a/jsbuiltin.c
+++ b/jsbuiltin.c
@@ -103,6 +103,11 @@
static const char *HEX = "0123456789ABCDEF";
+ if (js_try(J)) {
+ js_free(J, sb);
+ js_throw(J);
+ }
+
while (*str) {
int c = (unsigned char) *str++;
if (strchr(unescaped, c))
@@ -115,10 +120,6 @@
}
js_putc(J, &sb, 0);
- if (js_try(J)) {
- js_free(J, sb);
- js_throw(J);
- }
js_pushstring(J, sb ? sb->s : "");
js_endtry(J);
js_free(J, sb);
@@ -129,6 +130,11 @@
js_Buffer *sb = NULL;
int a, b;
+ if (js_try(J)) {
+ js_free(J, sb);
+ js_throw(J);
+ }
+
while (*str) {
int c = (unsigned char) *str++;
if (c != '%')
@@ -152,10 +158,6 @@
}
js_putc(J, &sb, 0);
- if (js_try(J)) {
- js_free(J, sb);
- js_throw(J);
- }
js_pushstring(J, sb ? sb->s : "");
js_endtry(J);
js_free(J, sb);
--- a/jsbuiltin.h
+++ b/jsbuiltin.h
@@ -18,33 +18,4 @@
void jsB_propn(js_State *J, const char *name, double number);
void jsB_props(js_State *J, const char *name, const char *string);
-typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
-
-static void js_putc(js_State *J, js_Buffer **sbp, int c)
-{
- js_Buffer *sb = *sbp;
- if (!sb) {
- sb = js_malloc(J, sizeof *sb);
- sb->n = 0;
- sb->m = sizeof sb->s;
- *sbp = sb;
- } else if (sb->n == sb->m) {
- sb = js_realloc(J, sb, (sb->m *= 2) + soffsetof(js_Buffer, s));
- *sbp = sb;
- }
- sb->s[sb->n++] = c;
-}
-
-static inline void js_puts(js_State *J, js_Buffer **sb, const char *s)
-{
- while (*s)
- js_putc(J, sb, *s++);
-}
-
-static inline void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e)
-{
- while (s < e)
- js_putc(J, sb, *s++);
-}
-
#endif
--- a/jsfunction.c
+++ b/jsfunction.c
@@ -12,6 +12,12 @@
js_Ast *parse;
js_Function *fun;
+ if (js_try(J)) {
+ js_free(J, sb);
+ jsP_freeparse(J);
+ js_throw(J);
+ }
+
/* p1, p2, ..., pn */
if (top > 2) {
for (i = 1; i < top - 1; ++i) {
@@ -24,12 +30,6 @@
/* body */
body = js_isdefined(J, top - 1) ? js_tostring(J, top - 1) : "";
-
- if (js_try(J)) {
- js_free(J, sb);
- jsP_freeparse(J);
- js_throw(J);
- }
parse = jsP_parsefunction(J, "[string]", sb ? sb->s : NULL, body);
fun = jsC_compilefunction(J, parse);
--- a/jsi.h
+++ b/jsi.h
@@ -142,6 +142,14 @@
#define js_trypc(J, PC) \
setjmp(js_savetrypc(J, PC))
+/* String buffer */
+
+typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
+
+void js_putc(js_State *J, js_Buffer **sbp, int c);
+void js_puts(js_State *J, js_Buffer **sb, const char *s);
+void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e);
+
/* State struct */
struct js_State
--- a/jsintern.c
+++ b/jsintern.c
@@ -1,5 +1,34 @@
#include "jsi.h"
+/* Dynamically grown string buffer */
+
+void js_putc(js_State *J, js_Buffer **sbp, int c)
+{
+ js_Buffer *sb = *sbp;
+ if (!sb) {
+ sb = js_malloc(J, sizeof *sb);
+ sb->n = 0;
+ sb->m = sizeof sb->s;
+ *sbp = sb;
+ } else if (sb->n == sb->m) {
+ sb = js_realloc(J, sb, (sb->m *= 2) + soffsetof(js_Buffer, s));
+ *sbp = sb;
+ }
+ sb->s[sb->n++] = c;
+}
+
+void js_puts(js_State *J, js_Buffer **sb, const char *s)
+{
+ while (*s)
+ js_putc(J, sb, *s++);
+}
+
+void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e)
+{
+ while (s < e)
+ js_putc(J, sb, *s++);
+}
+
/* Use an AA-tree to quickly look up interned strings. */
struct js_StringNode