You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
emscripten_run_script_string returns "null" instead of (char*)0 for null strings. #7291
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'.
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.
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.
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.
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.
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.
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"?
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.
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.
Activity
kripken commentedon Oct 15, 2018
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.
SteveBaker commentedon Oct 16, 2018
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.
kripken commentedon Oct 16, 2018
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.
SteveBaker commentedon Oct 31, 2018
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.
SteveBaker commentedon Oct 31, 2018
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.
SteveBaker commentedon Oct 31, 2018
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"?
SteveBaker commentedon Oct 31, 2018
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!
kripken commentedon Nov 1, 2018
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.
SteveBaker commentedon Nov 2, 2018
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.
Make emscripten_run_script_string return C NULL on null/undefined
Make emscripten_run_script_string return C NULL on null/undefined (#8793
Make emscripten_run_script_string return C NULL on null/undefined (em…