Cleanup patch geared toward not escaping control characters in XML literals (349814, r=mrbkap).

This commit is contained in:
brendan%mozilla.org 2006-09-05 20:21:56 +00:00
Родитель 21877dad03
Коммит ad16beafbf
4 изменённых файлов: 80 добавлений и 67 удалений

Просмотреть файл

@ -2435,10 +2435,8 @@ EmitElemOp(JSContext *cx, JSParseNode *pn, JSOp op, JSCodeGenerator *cg)
}
/* The right side of the descendant operator is implicitly quoted. */
if (op == JSOP_DESCENDANTS && right->pn_op == JSOP_STRING &&
js_NewSrcNote(cx, cg, SRC_UNQUOTE) < 0) {
return JS_FALSE;
}
JS_ASSERT(op != JSOP_DESCENDANTS || right->pn_type != TOK_STRING ||
right->pn_op == JSOP_QNAMEPART);
if (!js_EmitTree(cx, cg, right))
return JS_FALSE;
if (js_NewSrcNote2(cx, cg, SRC_PCBASE, CG_OFFSET(cg) - top) < 0)
@ -6032,11 +6030,11 @@ JS_FRIEND_DATA(JSSrcNoteSpec) js_SrcNoteSpec[] = {
{"while", 1, 0, 1},
{"for", 3, 1, 1},
{"continue", 0, 0, 0},
{"decl", 1, 0, 0},
{"decl", 1, 0, 1},
{"pcdelta", 1, 0, 1},
{"assignop", 0, 0, 0},
{"cond", 1, 0, 1},
{"unquote", 0, 0, 0},
{"unused10", 0, 0, 0},
{"hidden", 0, 0, 0},
{"pcbase", 1, 0, -1},
{"label", 1, 0, 0},

Просмотреть файл

@ -497,7 +497,7 @@ typedef enum JSSrcNoteType {
gets and sets */
SRC_ASSIGNOP = 8, /* += or another assign-op follows */
SRC_COND = 9, /* JSOP_IFEQ is from conditional ?: operator */
SRC_UNQUOTE = 10, /* don't quote a JSOP_STRING */
SRC_UNUSED10 = 10, /* unused */
SRC_HIDDEN = 11, /* opcode shouldn't be decompiled */
SRC_PCBASE = 12, /* distance back from annotated get- or setprop
op to first obj.prop.subprop bytecode */
@ -510,13 +510,19 @@ typedef enum JSSrcNoteType {
2nd off to first JSOP_CASE if condswitch */
SRC_FUNCDEF = 19, /* JSOP_NOP for function f() with atomid */
SRC_CATCH = 20, /* catch block has guard */
SRC_UNUSED21 = 21, /* Unused source note */
SRC_UNUSED21 = 21, /* unused */
SRC_NEWLINE = 22, /* bytecode follows a source newline */
SRC_SETLINE = 23, /* a file-absolute source line number note */
SRC_XDELTA = 24 /* 24-31 are for extended delta notes */
} JSSrcNoteType;
/* Constants for the SRC_DECL source note. */
/*
* Constants for the SRC_DECL source note. Note that span-dependent bytecode
* selection means that any SRC_DECL offset greater than SRC_DECL_LET may need
* to be adjusted, but these "offsets" are too small to span a span-dependent
* instruction, so can be used to denote distinct declaration syntaxes to the
* decompiler.
*/
#define SRC_DECL_VAR 0
#define SRC_DECL_CONST 1
#define SRC_DECL_LET 2

Просмотреть файл

@ -442,18 +442,22 @@ const jschar js_EscapeMap[] = {
0
};
#define QUOTE_IN_XML 0x10000
static char *
QuoteString(Sprinter *sp, JSString *str, jschar quote)
QuoteString(Sprinter *sp, JSString *str, uint32 quote)
{
JSBool inXML, ok;
jschar qc, c;
ptrdiff_t off, len, nb;
const jschar *s, *t, *u, *z;
char *bp;
jschar c;
JSBool ok;
/* Sample off first for later return value pointer computation. */
inXML = (quote & QUOTE_IN_XML) != 0;
qc = (jschar) quote;
off = sp->offset;
if (quote && Sprint(sp, "%c", (char)quote) < 0)
if (qc && Sprint(sp, "%c", (char)qc) < 0)
return NULL;
/* Loop control variables: z points at end of string sentinel. */
@ -462,7 +466,7 @@ QuoteString(Sprinter *sp, JSString *str, jschar quote)
for (t = s; t < z; s = ++t) {
/* Move t forward from s past un-quote-worthy characters. */
c = *t;
while (JS_ISPRINT(c) && c != quote && c != '\\' && !(c >> 8)) {
while (JS_ISPRINT(c) && c != qc && c != '\\' && !(c >> 8)) {
c = *++t;
if (t == z)
break;
@ -486,7 +490,9 @@ QuoteString(Sprinter *sp, JSString *str, jschar quote)
/* Use js_EscapeMap, \u, or \x only if necessary. */
if ((u = js_strchr(js_EscapeMap, c)) != NULL) {
ok = Sprint(sp, "\\%c", (char)u[1]) >= 0;
ok = inXML
? Sprint(sp, "%c", (char)c) >= 0
: Sprint(sp, "\\%c", (char)u[1]) >= 0;
} else {
#ifdef JS_C_STRINGS_ARE_UTF8
/* If this is a surrogate pair, make sure to print the pair. */
@ -496,6 +502,12 @@ QuoteString(Sprinter *sp, JSString *str, jschar quote)
buffer[1] = *++t;
buffer[2] = 0;
if (t == z) {
char numbuf[10];
JS_snprintf(numbuf, sizeof numbuf, "0x%x", c);
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_BAD_SURROGATE_CHAR,
numbuf);
ok = JS_FALSE;
break;
}
@ -505,7 +517,7 @@ QuoteString(Sprinter *sp, JSString *str, jschar quote)
ok = Sprint(sp, "%hc", c) >= 0;
}
#else
/* Use \uXXXX or \xXX if the string cannot be displayed as UTF-8. */
/* Use \uXXXX or \xXX if the string can't be displayed as UTF-8. */
ok = Sprint(sp, (c >> 8) ? "\\u%04X" : "\\x%02X", c) >= 0;
#endif
}
@ -514,7 +526,7 @@ QuoteString(Sprinter *sp, JSString *str, jschar quote)
}
/* Sprint the closing quote and return the quoted string. */
if (quote && Sprint(sp, "%c", (char)quote) < 0)
if (qc && Sprint(sp, "%c", (char)qc) < 0)
return NULL;
/*
@ -2498,11 +2510,8 @@ Decompile(SprintStack *ss, jsbytecode *pc, intN nb)
END_LITOPX_CASE
BEGIN_LITOPX_CASE(JSOP_STRING)
sn = js_GetSrcNote(jp->script, pc);
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
(jschar)((inXML ||
(sn && SN_TYPE(sn) == SRC_UNQUOTE))
? 0 : '"'));
inXML ? QUOTE_IN_XML : '"');
if (!rval)
return JS_FALSE;
todo = STR2OFF(&ss->sprinter, rval);

Просмотреть файл

@ -2841,7 +2841,7 @@ js_InflateString(JSContext *cx, const char *bytes, size_t *length)
if (!chars)
return NULL;
js_InflateStringToBuffer(cx, bytes, *length, chars, &dstlen);
chars [dstlen] = 0;
chars[dstlen] = 0;
*length = dstlen;
return chars;
}
@ -2854,13 +2854,13 @@ js_DeflateString(JSContext *cx, const jschar *chars, size_t length)
{
size_t size = 0;
char *bytes = NULL;
if (!js_DeflateStringToBuffer (cx, chars, length, NULL, &size))
if (!js_DeflateStringToBuffer(cx, chars, length, NULL, &size))
return NULL;
bytes = (char *) (cx ? JS_malloc(cx, size+1) : malloc(size+1));
if (!bytes)
return NULL;
js_DeflateStringToBuffer (cx, chars, length, bytes, &size);
bytes [size] = 0;
js_DeflateStringToBuffer(cx, chars, length, bytes, &size);
bytes[size] = 0;
return bytes;
}
@ -2907,7 +2907,7 @@ js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
goto bufferTooSmall;
if (dst) {
for (i = 0; i < utf8Len; i++)
*dst++ = (char) utf8buf [i];
*dst++ = (char) utf8buf[i];
}
}
dstlen -= utf8Len;
@ -2918,13 +2918,12 @@ js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
badSurrogate:
*dstlenp = (origDstlen - dstlen);
if (cx) {
char buffer [10];
JS_snprintf (buffer, 10, "0x%x", c);
JS_ReportErrorFlagsAndNumber(cx,
JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_BAD_SURROGATE_CHAR,
buffer);
char buffer[10];
JS_snprintf(buffer, 10, "0x%x", c);
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_BAD_SURROGATE_CHAR,
buffer);
}
return JS_FALSE;
@ -2958,7 +2957,7 @@ js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
if (n == 1 || n > 6)
goto badCharacter;
for (j = 1; j < n; j++) {
if ((src [j] & 0xC0) != 0x80)
if ((src[j] & 0xC0) != 0x80)
goto badCharacter;
}
v = Utf8ToOneUcs4Char(src, n);
@ -2967,13 +2966,13 @@ js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
if (v > 0xFFFFF || dstlen < 2) {
*dstlenp = (origDstlen - dstlen);
if (cx) {
char buffer [10];
JS_snprintf (buffer, 10, "0x%x", v + 0x10000);
char buffer[10];
JS_snprintf(buffer, 10, "0x%x", v + 0x10000);
JS_ReportErrorFlagsAndNumber(cx,
JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_UTF8_CHAR_TOO_LARGE,
buffer);
JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_UTF8_CHAR_TOO_LARGE,
buffer);
}
return JS_FALSE;
}
@ -3001,43 +3000,45 @@ js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
badCharacter:
*dstlenp = (origDstlen - dstlen);
if (cx) {
char buffer [10];
JS_snprintf (buffer, 10, "%d", offset);
JS_ReportErrorFlagsAndNumber(cx,
JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_MALFORMED_UTF8_CHAR,
buffer);
char buffer[10];
JS_snprintf(buffer, 10, "%d", offset);
JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR,
js_GetErrorMessage, NULL,
JSMSG_MALFORMED_UTF8_CHAR,
buffer);
}
return JS_FALSE;
bufferTooSmall:
*dstlenp = (origDstlen - dstlen);
if (cx)
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BUFFER_TOO_SMALL);
if (cx) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BUFFER_TOO_SMALL);
}
return JS_FALSE;
}
#else
JSBool
js_InflateStringToBuffer(JSContext* cx, const char *bytes, size_t length, jschar *chars, size_t* charsLength)
js_InflateStringToBuffer(JSContext* cx, const char *bytes, size_t length,
jschar *chars, size_t* charsLength)
{
size_t i;
if (length > *charsLength) {
for (i = 0; i < *charsLength; i++)
chars[i] = (unsigned char) bytes[i];
if (cx)
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BUFFER_TOO_SMALL);
if (cx) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BUFFER_TOO_SMALL);
}
return JS_FALSE;
}
else {
for (i = 0; i < length; i++)
chars[i] = (unsigned char) bytes[i];
*charsLength = length;
return JS_TRUE;
}
for (i = 0; i < length; i++)
chars[i] = (unsigned char) bytes[i];
*charsLength = length;
return JS_TRUE;
}
jschar *
@ -3053,13 +3054,14 @@ js_InflateString(JSContext *cx, const char *bytes, size_t *bytesLength)
}
for (i = 0; i < length; i++)
chars[i] = (unsigned char) bytes[i];
chars [length] = 0;
chars[length] = 0;
*bytesLength = length;
return chars;
}
JSBool
js_DeflateStringToBuffer(JSContext* cx, const jschar *chars, size_t length, char *bytes, size_t* bytesLength)
js_DeflateStringToBuffer(JSContext* cx, const jschar *chars, size_t length,
char *bytes, size_t* bytesLength)
{
size_t i;
@ -3072,12 +3074,10 @@ js_DeflateStringToBuffer(JSContext* cx, const jschar *chars, size_t length, char
}
return JS_FALSE;
}
else {
for (i = 0; i < length; i++)
bytes[i] = (char) chars[i];
*bytesLength = length;
return JS_TRUE;
}
for (i = 0; i < length; i++)
bytes[i] = (char) chars[i];
*bytesLength = length;
return JS_TRUE;
}
/*
@ -3097,7 +3097,7 @@ js_DeflateString(JSContext *cx, const jschar *chars, size_t length)
for (i = 0; i < length; i++)
bytes[i] = (char) chars[i];
bytes [length] = 0;
bytes[length] = 0;
return bytes;
}