News for hackers. Stuff that matters.
Re:My last name makes me invisible to Computers (2015) by DavidWoof on Friday August 18, 2017 @10:40AM (#15046578)
I've been programming for over a quarter century, and I feel pretty confident that I've never written anything that could confuse 'null' with NULL. I can't even think of a language that would let you easily do this.

If web forms aren't accepting NULL, then somebody probably specifically programmed the word 'null' into a filter of disallowed entries. Probably to stop clerks from entering the word 'null' to mean empty string. This has nothing to do with null being a reserved word in many languages, I'll bet the forms that aren't accepting 'null' aren't accepting 'none' or 'empty' either.

Re:My last name makes me invisible to Computers (2015) by ajuc on Friday August 18, 2017 @11:02AM (#15046795)
In java + operator on strings works like this:

    Object a = null;
    String s = "foo bar " + a; // s == "foo bar null"
So, it's possible to get "null" as a string downstream, when some variable that should be non-nulleable - was null. If you find such a bug and incompetently fix it by checking for "null" downstream instead of checking before turning variables into strings - you have the error from the article. Especially if you check ignoring the case.

BTW guess how I know it's working like that :)

Re:My last name makes me invisible to Computers (2015) by ufmace on Friday August 18, 2017 @11:46PM (#15051547)
That's insane.

I've known Java to be a bit verbose, but I thought it was mostly a reasonable language aside from that. I might have expected something like that from Javascript or PHP. Never in my wildest dreams would I have thought that boring old Java would interpret a null cast to a string as a literal string "null" when combining strings. Even Ruby and Python don't do that - they throw type mismatch errors instead. C# treats it as empty string.

Re:My last name makes me invisible to Computers (2015) by DSMan195276 on Saturday August 19, 2017 @09:20AM (#15053196)
Ehh, I don't really think this is a huge deal personally. Keep in mind that it is not that `(string)null` gives you the "null" string, or `((string)null).ToString()` gives you the null string. You have to use actual string concatenation to get it (Or as others have pointed out, `String.ToValue(null)`). Point being, it isn't/shouldn't be messing up your regular comparisons (Unless you turn it into a string beforehand), and you really shouldn't be checking for stuff like `null` against a string literal anyway - it's meant for displaying a human readable string, not an unambiguous string that could be converted back at a later time. If you wanted that then you should use proper serialization techniques rather then just concatenating a bunch of values.
Re:My last name makes me invisible to Computers (2015) by ufmace on Saturday August 19, 2017 @04:48PM (#15055510)
It's not that it's really that huge of a deal, just that it's so out of character for Java and everything it is seen as.

It's like if a guy you were friends with since college and always knew to be solid and reliable but a little boring, and now many years later, he's happily married with kids, watching sports, working in a large hierarchical organization, doing pretty average stuff but nothing unusual or exciting. If you suddenly found out that guy had secretly been a Furry the whole time, that would be really shocking.

Not that there's anything wrong with being boring and reliable, or being a furry. But the sudden change in how you saw somebody or something is stunning.

I half expect anything written in Java to be littered with AbstractFactoryFactories and giant frameworks using 20 different design patterns to write Hello World. I never would have expected Java to silently convert an actual null to the string "null". I always thought the one thing you can count on Java for was to be strictly strongly typed, and never silently do any weird random conversions that nobody would have expected. Guess I was wrong.

Though poking around in a few other languages, JavaScript does indeed do that also, though I kinda expect JavaScript to do things like that. In Ruby, nil.to_s gives empty string, and adding a nil to a string gives you a type error. In Python, trying to add None to a string also gives you a type error, but str(None) does give you 'None'. That's a bit disappointing, but not shocking to me.

Re:My last name makes me invisible to Computers (2015) by Zak on Saturday August 19, 2017 @11:57AM (#15054051)
It's insane in that Java pretends to have strong, static typing, then does things like that.
Re:My last name makes me invisible to Computers (2015) by Spivak on Saturday August 19, 2017 @08:09PM (#15056301)
Overloading the plus operator for strings was probably a mistake in the language but that's hardly a condemnation of their type system.
Re:My last name makes me invisible to Computers (2015) by Zak on Saturday August 19, 2017 @09:31PM (#15056532)
A lot of languages overload + to mean concatenate. I think that's a mistake, however...

In Python:

    "abc" + None
    TypeError: cannot concatenate 'str' and 'NoneType' objects
    
    "abc".join(None)
    TypeError: can only join an iterable
In Ruby

    "abc" + nil
    TypeError: no implicit conversion of nil into String
In Lua

    = "abc" .. Nil
    attempt to concatenate global 'Nil' (a nil value)
But not Java. Java implicitly tries to coerce the provided value to be a string, which seems out of place in a language that values type safety. That all types might also be null also seems out of place in a language with strong, static typing, but that's been discussed to death. The problem is compounded by the fact that null is actually converted to "null" instead of an empty string.
Re:My last name makes me invisible to Computers (2015) by Too on Sunday August 20, 2017 @04:52AM (#15057568)
On the other hand, it's easier to get into this situation in Python than Java.

    bar = "bar" 
    foo = "foo"
    foo_bar = foo + baar   # intentional typo (baar will be None)
In Java this would be a compiler error, but python accepts it until the program is being run.
Re:My last name makes me invisible to Computers (2015) by Zak on Sunday August 20, 2017 @09:05AM (#15058249)
That's a NameError in Python: undefined variables don't have a value, not even None. It won't be caught until runtime since there's no AOT compiler in Python, but it's the same kind of error it is in Java.

My claim isn't that Python is safer overall than Java. Instead, it's that Java, a language that is mostly type safe, most of the time should not have these two potentially surprising behaviors:

1. The standard string concatenation operator does implicit coercion rather than rejecting an input that isn't a string. There should be a builtin to make a string from any value no matter what for logging and debugging, but that shouldn't be the standard concatenation operator.

2. This is more controversial, but strongly statically typed languages should not allow arbitrary values to be null. That sabotages one of the major strengths of strong static typing. Instead, there should be an option type to make it explicit. For something familiar to most programmers, SQL does this.

Re:My last name makes me invisible to Computers (2015) by anotherbrownguy on Saturday August 19, 2017 @12:12PM (#15054133)
>I might have expected something like that from Javascript or PHP.

Javascript does it too, but PHP doesn't.

Re:My last name makes me invisible to Computers (2015) by tim333 on Saturday August 19, 2017 @07:55AM (#15052885)
Trying it in the console just now I see javascript does it too

    >"hey "+null
    "hey null"
I wonder why the language designers thought that was a good idea?
Re:My last name makes me invisible to Computers (2015) by kpil on Saturday August 19, 2017 @09:08AM (#15053144)
The reason is that the string concatenation operation is doing a String.valueOf() on the argument, and that delegates to toString() that promises a human readable string that is relevant.

For some reason someone decided to check for null instead of the stricter option of throwing a NullPointerException. Maybe to help debugging or to follow the gist of toString().

I guess it was too late to change even in Java 0.9... Autoboxing, iterators and other newer features are more strict,so I suppose it's just one of the few irregularities left from prehistoric times...

Re:My last name makes me invisible to Computers (2015) by rootlocus on Saturday August 19, 2017 @06:51AM (#15052709)
> it's possible to get "null" as a string downstream

I don't see the problem with that. Strings that contain formatted variables should be used for display only. Besides, when someone inputs his name, the input is already a string, and I don't see how you would dereference string contents.

Re:My last name makes me invisible to Computers (2015) by amptorn on Saturday August 19, 2017 @11:59AM (#15054061)
That's not the problem. Strings saying "null" are not the problem. That's just four lower-case characters. No system chokes on that directly.

The problem is downstream, where some galactic idiot has special-cased the string "null" to turn back into an actual `null`.

Re:My last name makes me invisible to Computers (2015) by incongruity on Friday August 18, 2017 @11:19AM (#15046960)
Sure – that's just casting a null type as a string.

So I guess if you have a comparison that somehow casts null to a string before comparing, you could run into this issue, but that's still bad programming.

I used to own [email protected] as an email alias (with my real university, not that generic .edu, of course) and I got all sorts of interesting things... but that was very intentional on my part.

Re:My last name makes me invisible to Computers (2015) by ajuc on Friday August 18, 2017 @12:23PM (#15047546)
Funnily enough - it's not the null-> String conversion, it's the "+" operator. That was at one point subject of heated debate in my previous job :)

See:

     "null".equals((String)null)  //false
     "null".equals((String)null+"") //true
     System.out.print((String)null) // throws NPE
     System.out.println((String)null) // prints "null", I guess because it appends "\n" inside
Re:My last name makes me invisible to Computers (2015) by incongruity on Friday August 18, 2017 @12:50PM (#15047765)
Right, but that operator is implicitly casting the null object to a string type – it has to, in a strict sense... some other languages would raise an error (and many would do the same as Java).
Re:My last name makes me invisible to Computers (2015) by ajuc on Friday August 18, 2017 @01:01PM (#15047853)
It doesn't have to. null.toString() throws NPE as it should. I would expect "foo" + null to throw NPE as well.

BTW there's another "fun" gotcha, when you interface java code and oracle database which consider empty string and null to be the same thing. Depending on how you handle data from database you end up with null, "", or "null" :)

Re:My last name makes me invisible to Computers (2015) by Asooka on Friday August 18, 2017 @05:46PM (#15050010)
String.valueOf(null) returns "null" though, and that is what "foo" + null uses. I can understand why it would be surprising if you thought it used .toString, but it's clearly listed in the standard.
Re:My last name makes me invisible to Computers (2015) by LukeShu on Friday August 18, 2017 @11:57PM (#15051577)
Sure, but .toString() can be used to change how your class stringifies, so clearly it is being called at some point (perhaps by String.valueOf()).

And indeed,

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/s...
Re:My last name makes me invisible to Computers (2015) by incongruity on Friday August 18, 2017 @03:46PM (#15049167)
Interesting – I haven't touched Java in years so I'm definitely out of my element with that language, so thank you.
Re:My last name makes me invisible to Computers (2015) by amenghra on Friday August 18, 2017 @06:36PM (#15050291)

    System.out.print((String)null)
Does not throw NPE.
Re:My last name makes me invisible to Computers (2015) by ajuc on Friday August 18, 2017 @06:58PM (#15050396)
it does here:

http://www.javarepl.com/term.html

But you're right, in regular java environment it doesn't now that I've checked.

Re:My last name makes me invisible to Computers (2015) by monsieurbanana on Friday August 18, 2017 @11:41AM (#15047147)
I don't think anyone ever argued that this wasn't because of bad programming.
Re:My last name makes me invisible to Computers (2015) by jameshart on Friday August 18, 2017 @05:34PM (#15049918)
Often it'll be compatibility with some upstream legacy system which exposes data as CSV or tab delimited, and outputs data like this:

   First,Middle,Last
   Alice,Q,Foo
   Bob,NULL,Bar
In a world like that, some code got written in the wrong tier to output

   value.toUpper() == "NULL" ? "" : value
and you wind up with a web form that can't roundtrip a name that contains the word 'null'.
Re:My last name makes me invisible to Computers (2015) by Animats on Saturday August 19, 2017 @12:11AM (#15051622)
One place where that can easily happen is loading comma-separated value files into a database. Some CSV files are formatted with the convention that fields only appear in quotes if they contain special characters. Thus, a data value of NULL is not quoted. This loses the distinction made in SQL databases between NULL, the null value, and "NULL", the string.

(I just received some files like this. One is a file that has names. If someone had a name of NULL, it would go into the database as a null.)

Re:My last name makes me invisible to Computers (2015) by tyingq on Friday August 18, 2017 @10:47AM (#15046650)
Here's an example where it creeps in: https://issues.apache.org/jira/browse/FLEX-33644
Re:My last name makes me invisible to Computers (2015) by jey on Friday August 18, 2017 @11:13AM (#15046882)
Relevant excerpt:

    var nullXML:XML = <root>null</root>;
    if (nullXML == null) { trace("Some XML is null"); }
    
    nullXML == null is true!
That's... impressive.
Re:My last name makes me invisible to Computers (2015) by tyingq on Friday August 18, 2017 @11:18AM (#15046941)
Bug is still unclosed too.
Re:My last name makes me invisible to Computers (2015) by DavidWoof on Friday August 18, 2017 @11:02AM (#15046788)
OK, that's a good one.

Is this ActionScript that's doing the weird xml tag interpolation?

I stand corrected, this is definitely possible. Still, I bet this person's real problems stem from explicit coding.

Re:My last name makes me invisible to Computers (2015) by zoul on Friday August 18, 2017 @10:45AM (#15046627)
String interpolation will often end up crossing the barrier between NULL as an empty pointer value and "null" as an ordinary string.
Re:My last name makes me invisible to Computers (2015) by benmmurphy on Friday August 18, 2017 @11:39AM (#15047133)
i just checked one of the projects where i work and a search for "null" returns 32 matches. its a java project and i believe the checks came about because stuff being converted to strings and then being sent back and forth between html forms and the backend.

    > git grep -i '"NULL"' src |wc -l
    > 32
Re:My last name makes me invisible to Computers (2015) by boondaburrah on Friday August 18, 2017 @10:56AM (#15046725)
It's probably old DB software, the interactions between, and not the language itself.
Re:My last name makes me invisible to Computers (2015) by crdoconnor on Friday August 18, 2017 @08:27PM (#15050811)
YAML does this.

It's one of the reasons I wrote a pared down 'dumb' YAML parser that assumes scalar values are strings unless directed otherwise: https://github.com/crdoconnor/strictyaml

Re:My last name makes me invisible to Computers (2015) by jugg1es on Friday August 18, 2017 @10:51AM (#15046682)
Javascript lets you do it if you use the equality (==) instead of the identity (===).. i think.
Re:My last name makes me invisible to Computers (2015) by faceplanted on Friday August 18, 2017 @10:57AM (#15046744)
Javascript lets you do anything wrong if you use == and not === though, I'm pretty sure the machine apocalypse is going to happen because someone types == instead of === at this point.
Re:My last name makes me invisible to Computers (2015) by TallGuyShort on Friday August 18, 2017 @11:31AM (#15047055)
At least if the Terminators are running JavaScript they'll be pretty easy to thwart.
Re:My last name makes me invisible to Computers (2015) by unkown-unknowns on Saturday August 19, 2017 @12:20PM (#15054172)
Blackhat conference talk we might see in the future: "How I achieved remote code execution on the T-1000 and singlehandedly averted the extinction of the human race"
Re:My last name makes me invisible to Computers (2015) by nulagrithom on Friday August 18, 2017 @03:53PM (#15049232)
Nope. Not even JavaScript screws this one up. You've got to be doing something stupid with strings to get tripped up by "Null".
Re:My last name makes me invisible to Computers (2015) by swalsh on Friday August 18, 2017 @09:18PM (#15051018)
Imagine a protocol that uses xml.... there's a few out. There are no quotes so null as a string will look exactly the same as A NULL value.
Re:My last name makes me invisible to Computers (2015) by kpil on Saturday August 19, 2017 @09:14AM (#15053167)
But that's just the tip of the iceberg that xml is a amateurish protocol language. I'm so glad that it's finally dying.

Unfortunately it's replaced by something even more messed up

Re:My last name makes me invisible to Computers (2015) by ams6110 on Friday August 18, 2017 @10:09PM (#15051240)
Given that the entire protocol of the web (html and http) is string-based, that's not very unlikely.
Re:My last name makes me invisible to Computers (2015) by profmonocle on Friday August 18, 2017 @10:58AM (#15046748)
Just tested (in the node CLI console) and that doesn't seem to be the case.

Edit: It's true in PHP though. :-/

Re:My last name makes me invisible to Computers (2015) by tyingq on Friday August 18, 2017 @11:05AM (#15046816)
>Edit: It's true in PHP though. :-/

Can you post exactly what you did in PHP that shows "NULL" is equal to NULL? There's quite a few approaches like ==, ===, and is_null(). I can't get any to think "NULL" is NULL, though I imagine I'm missing something.

Re:My last name makes me invisible to Computers (2015) by fenwick67 on Friday August 18, 2017 @12:03PM (#15047341)
null == undefined in js.
Re:My last name makes me invisible to Computers (2015) by MattBearman on Saturday August 19, 2017 @03:18AM (#15052143)
When using sequel pro with a MySQL database, typing NULL into a string field will make that field null rather than 'null', which always seemed like an odd design choice to me
Re:My last name makes me invisible to Computers (2015) by brak1 on Saturday August 19, 2017 @04:29AM (#15052338)
Its a good compromise i think. OTherwise to update a field to null it would need a button or right clicking and selected a null option. I would expect the amount of people who actually want to enter a 'null' string is minimal...
Re:My last name makes me invisible to Computers (2015) by prance on Saturday August 19, 2017 @06:20AM (#15052635)
This reasoning is probably behind some the OP's cases...
Re:My last name makes me invisible to Computers (2015) by Semaphor on Saturday August 19, 2017 @05:39AM (#15052550)
Yeah, Null is no problem. But thanks to code my predecessor wrote, >null< would be.
Re:My last name makes me invisible to Computers (2015) by __s on Sunday August 20, 2017 @09:19AM (#15058299)
SQL abstraction layer at work in PHP special cases the string NULL to search for 'is null' rather than search for the string. So it happens
Re:My last name makes me invisible to Computers (2015) by jlebrech on Friday August 18, 2017 @10:53AM (#15046696)
I can only see that happen if eval was ever used.
Re:My last name makes me invisible to Computers (2015) by ams6110 on Friday August 18, 2017 @10:11PM (#15051246)
Eval used to be used a lot, because web forms send everything as strings and you need to convert them to whatever datatypes on the back end. Eval probably permeates a lot of older legacy systems.
Re:My last name makes me invisible to Computers (2015) by calibas on Friday August 18, 2017 @11:36PM (#15051504)
I imagine there's many special cases where a compiler's type coercion creates an issue.