diff --git a/package/gluon-web/src/template_lualib.c b/package/gluon-web/src/template_lualib.c index ac93ab68..4af2f777 100644 --- a/package/gluon-web/src/template_lualib.c +++ b/package/gluon-web/src/template_lualib.c @@ -75,19 +75,16 @@ static int template_L_parse_string(lua_State *L) static int template_L_pcdata(lua_State *L) { - size_t len = 0, outlen; - const char *str = luaL_checklstring(L, 1, &len); - char *res = pcdata(str, len, &outlen); + size_t inlen, outlen; + char *out; + const char *in = luaL_checklstring(L, 1, &inlen); + if (!pcdata(in, inlen, &out, &outlen)) + return 0; - if (res != NULL) - { - lua_pushlstring(L, res, outlen); - free(res); + lua_pushlstring(L, out, outlen); + free(out); - return 1; - } - - return 0; + return 1; } static int template_L_load_catalog(lua_State *L) { diff --git a/package/gluon-web/src/template_utils.c b/package/gluon-web/src/template_utils.c index 8a4cecfe..f8ea276f 100644 --- a/package/gluon-web/src/template_utils.c +++ b/package/gluon-web/src/template_utils.c @@ -256,7 +256,7 @@ static size_t validate_utf8(const unsigned char **s, size_t l, struct template_b /* Sanitize given string and strip all invalid XML bytes * Validate UTF-8 sequences * Escape XML control chars */ -char * pcdata(const char *s, size_t l, size_t *outl) +bool pcdata(const char *s, size_t l, char **out, size_t *outl) { struct template_buffer *buf = buf_init(l); const unsigned char *ptr = (const unsigned char *)s; @@ -265,15 +265,14 @@ char * pcdata(const char *s, size_t l, size_t *outl) int esl; if (!buf) - return NULL; + return false; for (o = 0; o < l; o++) { /* Invalid XML bytes */ if ((*ptr <= 0x08) || ((*ptr >= 0x0B) && (*ptr <= 0x0C)) || ((*ptr >= 0x0E) && (*ptr <= 0x1F)) || - (*ptr == 0x7F)) - { + (*ptr == 0x7F)) { ptr++; } @@ -282,8 +281,7 @@ char * pcdata(const char *s, size_t l, size_t *outl) (*ptr == '"') || (*ptr == '&') || (*ptr == '<') || - (*ptr == '>')) - { + (*ptr == '>')) { esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr); if (!buf_append(buf, esq, esl)) @@ -293,14 +291,12 @@ char * pcdata(const char *s, size_t l, size_t *outl) } /* ascii char */ - else if (*ptr <= 0x7F) - { + else if (*ptr <= 0x7F) { buf_putchar(buf, (char)*ptr++); } /* multi byte sequence */ - else - { + else { if (!(v = validate_utf8(&ptr, l - o, buf))) break; @@ -309,5 +305,6 @@ char * pcdata(const char *s, size_t l, size_t *outl) } *outl = buf_length(buf); - return buf_destroy(buf); + *out = buf_destroy(buf); + return true; } diff --git a/package/gluon-web/src/template_utils.h b/package/gluon-web/src/template_utils.h index 35bf706d..6dc18414 100644 --- a/package/gluon-web/src/template_utils.h +++ b/package/gluon-web/src/template_utils.h @@ -42,7 +42,6 @@ static inline size_t buf_length(const struct template_buffer *buf) return buf->dptr - buf->data; } - -char * pcdata(const char *s, size_t l, size_t *outl); +bool pcdata(const char *s, size_t l, char **out, size_t *outl); #endif