Page MenuHomePhabricator

[analyzer] Fix zero-initialization of stack VLAs under ARC.
ClosedPublic

Authored by NoQ on Dec 20 2017, 7:53 PM.

Details

Summary

https://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW5 :

Using ARC, strong, weak, and autoreleasing stack variables are now implicitly initialized with nil.

This includes variable-length arrays of Objective-C object pointers. However, in the analyzer we don't zero-initialize them. We used to, but it accidentally regressed after r289618.

Under ARC, the array variable's initializer within DeclStmt is an ImplicitValueInitExpr. Environment doesn't maintain any bindings for this expression kind - instead it always knows that it's a known constant (0 in our case), so it just returns the known value by calling SValBuilder::makeZeroVal() (see EnvironmentManager::getSVal(). Commit r289618 had introduced reasonable behavior of SValBuilder::makeZeroVal() for the arrays, which produces a zero-length compoundVal{}. When such value is bound to arrays, in RegionStoreManager::bindArray() "remaining" items in the array are default-initialized with zero, as in RegionStoreManager::setImplicitDefaultValue(). The similar mechanism works when an array is initialized by an initializer list that is too short, eg. int a[3] = { 1, 2 }; would result in a[2] initialized with 0. However, in case of variable-length arrays it didn't know if any more items need to be added, because, well, the length is variable.

Add the default binding anyway, regardless of how many actually need to be added. We don't really care, because the default binding covers the whole array anyway.

Diff Detail

Repository
rC Clang

Event Timeline

NoQ created this revision.Dec 20 2017, 7:53 PM
dcoughlin accepted this revision.Dec 21 2017, 10:24 AM

LGTM. The tests are great!!

This revision is now accepted and ready to land.Dec 21 2017, 10:24 AM
This revision was automatically updated to reflect the committed changes.