Skip to content

emscripten_run_script_string returns "null" instead of (char*)0 for null strings. #7291

Closed

Description

@SteveBaker

If emscripten_run_script_string runs a command that returns a "null" string (in my case, the JavaScript command was: "localStorage.getItem('foobar');" where there is no such item as 'foobar') - it returns a valid string containing the letters 'n' 'u' 'l' 'l' rather than returning a C/C++ NULL. Consequence being that you can't tell whether you have an actual missing item or an item that happens to contain the string 'null'.

Activity

  1. kripken commented on Oct 15, 2018

    @kripken
    Member

    This API always returns a string, by converting the JS output to a string first. So that is the currently expected behavior. However, perhaps it does make sense to return a NULL in C when the result is a JS falsey value..? Not sure.

  2. SteveBaker commented on Oct 16, 2018

    @SteveBaker
    Author

    The problem being - if the string truly is "null" (four ASCII characters) and not a null string - there is no way to tell - and since a null string is typically some kind of an error condition (as in the case of reading a localStorage item) - and "null" is a perfectly valid string - we're going to be seriously screwed (and I was!) - the idea, surely is to return a C string that represents the result of the JavaScript code (after all, we do stick a '\0' on the end - so it's not literally what JavaScript returns) - and this behavior quite utterly fails to capture that. If that's what the spec says - then we need to change the spec because (a) it's not useful and (b) it's not what any sane person would expect it to do! The way it works now is just asking for trouble down the line when some malware idiot starts putting "null" into strings and watching the C code fail to handle it.

  3. kripken commented on Oct 16, 2018

    @kripken
    Member

    Maybe you're right, I'm not sure. Would be good to have more people's feedback here.

    I'd summarize the current function as "evaluate the input as JS, convert the result to a string, and return that string", while you're suggesting it should be "evaluate the input as JS, return a string if it is a string, otherwise return NULL". I think each makes sense in its own way - perhaps a new API for the latter makes sense.

  4. SteveBaker commented on Oct 31, 2018

    @SteveBaker
    Author

    The problem (and it is HUGE) is that far FAR too many things you might want to call in JS return a null on error.
    What we're doing by converting a null into a "null" is preventing wasm programs from detecting error conditions on the JS side. Preventing programs from detecting errors is really pretty terrible behavior.
    This isn't a matter of opinion - the way it works now is undoubtedly WRONG.

    Worse still - my only defense against the current behavior is to compare the string I get back against the string "null" with a string compare. This is slow - and it's error-prone because the string inside JS might actually BE the four letters n, u, l, l and not a null string - and what this stupid code is doing is making it 100% IMPOSSIBLE for me to distinguish an error condition from a perfectly legitimate piece of text.

    So - there really shouldn't be any discussion about this. It's a bug. It's a bad decision. It NEEDS to be fixed ASAP before we have a bajillon if ( strcmp ( JSresult, "null" ) )... things appearing in wasm programs.

    I'm frankly horrified that there is any debate about this.

  5. SteveBaker commented on Oct 31, 2018

    @SteveBaker
    Author

    This also violates the "principle of least surprise" - so when I call a JS function that says it returns a null string - it damned well ought to return a null string - and not four ASCII characters. I can imagine any C/C++ programmer EXPECTING it to do anything other than returning a NULL.

  6. SteveBaker commented on Oct 31, 2018

    @SteveBaker
    Author

    Without this change - how would you propose I rewrite this super-simple bit of C++ code?

    char *result = emscripten_run_script_string ( "localStorage.getItem('foobar');" ) ;

    if ( result )
    printf ( "foobar is '%s'\n", result ) ;
    else
    printf ( "**ERROR: Missing 'foobar'\n" ) ;

    localStorage.getItem returns a null string if there is no field 'foobar' in local storage. How can I tell whether 'foobar' is nonexistent or whether it contains the string "null"?

  7. SteveBaker commented on Oct 31, 2018

    @SteveBaker
    Author

    What is the use-case for WANTING this function to return "null" for a null result? I can't think of a single case when that's desirable.

    I apologize if I sound angry...but it's mostly because I'm angry!

  8. kripken commented on Nov 1, 2018

    @kripken
    Member

    Those are fair points. And the old behavior is always possible if the user does toString() or such. So I would be ok with a PR making this change, if we properly document it as a breaking change (in the changelog), and add tests.

    If you work on such a PR, let me know if you run into any problems I can help with.

  9. SteveBaker commented on Nov 2, 2018

    @SteveBaker
    Author

    Many thanks! I'm not sufficiently familiar with the innards of emscripten - or your test protocols - to attack this one myself - but for anyone familiar with the code, I expect it should be an easy fix.

  10. added a commit that references this issue on Jun 13, 2019
    bc0434a
  11. added a commit that references this issue on Jun 18, 2019
    a2d89ad
  12. added a commit that references this issue on Dec 23, 2020
    be44136
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions