shithub: libmujs

Download patch

ref: 3ceaeb153d9c324bfe73209b6beb0e2768ee59cb
parent: 1a1d26692103d476af41fd9f6bf69cda3d488271
author: Tor Andersson <tor.andersson@artifex.com>
date: Mon Jan 5 12:09:46 EST 2015

Use manual loop instead of memcpy/strcpy when creating short strings.

Due to our slightly icky struct layout, some versions of gcc barf and
generate crashing code when using strcpy and memcpy. I suspect gcc tries
to do some weird optimizations that fail because we intentionally
overwrite the 'pad' data in the js_Value struct for short strings.

--- a/jsrun.c
+++ b/jsrun.c
@@ -106,9 +106,10 @@
 	int n = strlen(v);
 	CHECKSTACK(1);
 	if (n < 16) {
+		char *s = STACK[TOP].u.shrstr;
+		while (n--) *s++ = *v++;
+		*s = 0;
 		STACK[TOP].type = JS_TSHRSTR;
-		memcpy(STACK[TOP].u.shrstr, v, n);
-		STACK[TOP].u.shrstr[n] = 0;
 	} else {
 		STACK[TOP].type = JS_TMEMSTR;
 		STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
@@ -120,9 +121,10 @@
 {
 	CHECKSTACK(1);
 	if (n < 16) {
+		char *s = STACK[TOP].u.shrstr;
+		while (n--) *s++ = *v++;
+		*s = 0;
 		STACK[TOP].type = JS_TSHRSTR;
-		memcpy(STACK[TOP].u.shrstr, v, n);
-		STACK[TOP].u.shrstr[n] = 0;
 	} else {
 		STACK[TOP].type = JS_TMEMSTR;
 		STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);