portal entry

select a category, or use search below
(searches all categories and all time range)

type checking cannot be trusted

| View in Portal
June 13, 2018 07:50:06 AM GMT
<p>Type checking cannot be trusted. Repro: <cfscript> function f1(numeric myNumeric) { return min(myNumeric, 1); } function f2(numeric myNumeric) { return myNumeric.min(1); } </cfscript> <cfscript> function f3(date myDate) { return dateAadd(“d”, 1, myDate); } function f4(date myDate) { return myDate.add(“d”, 1); } </cfscript> I can break someone’s f2 by passing a date to the numeric argument. I can break someone’s f4 by passing a numeric to the date argument. Look at f1 vs f2 and f3 vs f4. See the only […]</p>
<p>The post <a rel="nofollow" href="https://coldfusion.adobe.com/2018/06/type-checking-cannot-be-trusted/">type checking cannot be trusted</a> appeared first on <a rel="nofollow" href="https://coldfusion.adobe.com">ColdFusion</a>.</p>
Labels: Discussion, member function, typing, udf


Isn't date floating point number after certain fixed point in time? I have seen myDate + 1 be used without error. Is that like adding one day?
Comment by James Mohler
1131 | June 19, 2018 04:28:06 PM GMT
I wonder if myDate++ works. Should it even work?
Comment by James Mohler
1130 | June 19, 2018 04:28:58 PM GMT
Hi James, Yes, myDate+1/myDate++ are shorthand for adding 1 day: - http://www.forta.com/blog/index.cfm?mode=entry&entry=5A1DE7BC-3048-80A9-EF7E090A7E99B257 - CF10 example: https://trycf.com/gist/614f88c32797f621ab8cfa9e57250459/acf?theme=monokai That's why, when cfloop'ing over a date range, the index value is actually a number. And since CF is* type-casting, min(foo, 1) works even when foo is a date. And dateAdd(foo, "d", 1) works even when foo is a number. And, side-note, isValid("string", now()) has always returned true, and a string argument has always allowed a date. * Issue: Currently, member functions are type-specific, not type-casting. Meaning, foo.min(1) errors if foo is a date, and foo.add("d", 1) errors if foo is a number. And if a foo date passes thru a string argument (ex: an argument that accepts date objects and formatted date strings), foo.trim() errors. This means developers cannot use member functions in many cases, unless they add additional/unnecessary checks to determine the underlying Java type (java.lang.string, coldfusion.runtime.OleDateTime, java.lang.Double, etc) before using a member function. Question: member functions should be type-casting like the rest of CF, yes? Thanks!, -Aaron
Comment by Aaron Neff
1135 | June 19, 2018 11:14:47 PM GMT
Success: function f5(string myDateTime) { if(isValid("date", trim(myDateTime))) { writeOutput(dateAdd("m", 1, trim(myDateTime))); } } f5(now()) f5("2018-01-01T00:00:00Z") Failure: function f6(string myDateTime) { if(isValid("date", myDateTime.trim())) { writeOutput(myDateTime.trim().add("m", 1)); } } f6(now()) f6("2018-01-01T00:00:00Z") IMO, both are completely legit yes? Adobe? Thanks!, -Aaron
Comment by Aaron Neff
1136 | June 19, 2018 11:31:55 PM GMT