Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created June 20, 2011 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/1035504 to your computer and use it in GitHub Desktop.
Save bnoordhuis/1035504 to your computer and use it in GitHub Desktop.
V8 data corruption
'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
.split('')
.map(function(c) { return c.charCodeAt(0); })
.map(String.fromCharCode); // corrupts data
.join('');
// produces 'W\u0000\u0000i\u0001\u0000l\u0002\u0000l\u0003\u0000s\u0004\u0000t\u0005\u0000 \u0006\u0000d\u0007\u0000u\b\u0000 \t\u0000d\n\u0000i\u000b\u0000e\f\u0000 \r\u0000B\u000e\u0000l\u000f\u0000ü\u0010\u0000t\u0011\u0000h\u0012\u0000e\u0013\u0000 \u0014\u0000d\u0015\u0000e\u0016\u0000s\u0017\u0000 \u0018\u0000f\u0019\u0000r\u001a\u0000ü\u001b\u0000h\u001c\u0000e\u001d\u0000n\u001e\u0000,\u001f\u0000 \u0000d!\u0000i"\u0000e#\u0000 $\u0000F%\u0000r&\u0000ü\'\u0000c(\u0000h)\u0000t*\u0000e+\u0000 ,\u0000d-\u0000e.\u0000s/\u0000 0\u0000s1\u0000p2\u0000ä3\u0000t4\u0000e5\u0000r6\u0000e7\u0000n8\u0000 9\u0000J:\u0000a;\u0000h<\u0000r=\u0000e>\u0000s?\u0000'
cs = 'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'.split('').map(function(c) { return c.charCodeAt(0); });
s = [];
for (var i = 0; i < cs.length; ++i) s.push(String.fromCharCode(cs[i]));
s.join('');
// produces 'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
@mathiasbynens
Copy link

@bnoordhuis
Copy link
Author

It was a bug in the test, Array.map passes several arguments to its callback and String.fromCharCode was happily converting that to garbage. The example below works:

'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
  .split('')
  .map(function(c) { return c.charCodeAt(0); })
  .map(function(n) { return String.fromCharCode(n); })
  .join('');

// produces 'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'

@mathiasbynens
Copy link

I see; thanks for the follow-up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment