Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Subtle issue in manual on interacting with browser scripting

Discussion in 'Web' started by diekeure, Nov 18, 2015.

  1. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    221
    Hello,

    I just solved a bug we had in our game, and it is possibly present in this manual page.

    In the manual we have this function:

    Code (csharp):
    1. StringReturnValueFunction: function()
    2. {
    3.  var returnStr = "bla";
    4.  var buffer = _malloc(returnStr.length + 1);
    5.   writeStringToMemory(returnStr, buffer);
    6.   return buffer;
    7. },
    The problem is that _malloc creates a buffer of x bytes, but while a string can be x characters long, it is still possible that the characters take multiple bytes each. This causes the function writeStringToMemory to write outside of the buffer!

    I found this function to return the correct amount of bytes for a string:

    Code (csharp):
    1. getUTF8Length: function(str)
    2. {
    3.   var s = str.length;
    4.   for (var i=str.length-1; i>=0; i--) {
    5.     var code = str.charCodeAt(i);
    6.     if (code > 0x7f && code <= 0x7ff) s++;
    7.     else if (code > 0x7ff && code <= 0xffff) s+=2;
    8.     if (code >= 0xDC00 && code <= 0xDFFF) i--;
    9.   }
    10.   return s;
    11. },
    Just mentioning it here in case somebody faces the same issue as I did.

    There's more details in this blog post I wrote.

    Grtz,
    Alex
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Good catch, thanks (@kripken : it seems the emscripten docs also have this wrong, see writeStringToMemory in https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html ).

    I will update our docs with the right way to do it. FWIW, you should be able to use emscripten's lengthBytesUTF8(str) function to get the size, no need to write your own (and the emscripten one will also handle characters larger then two bytes correctly).
     
  3. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    221
    Ah thanks, that function is indeed perfect!