The extent calculation function had a bug which caused it to ignore if the size value was defined prior to casting it. As a result, size expressions with free variables would trigger assertion failures during the cast operation.
This patch adds that missing check, and replaces the redundant call to castAs<>() with the SVar that is returned by the checked cast.
Added a regression test "Malloc+NewDynamicArray" that exercises the fix.