- hey @marthakelly have you met @garannm? she is also a big javascript fan... 8 hours ago
- woot looks like i get to spend a week working with the Know Your Meme team haxoring on some ruby in the near future. 8 hours ago
- Cheezburger is hiring C# devs. If you are lol-curious you should apply. cheezburger.theresumator.com/apply/NONdUl/A… 12 hours ago
- My weapon would be a ghost. #Blackwater 2 days ago
- RT @Cheezburger: Stick 'Em with the pointy end. A Game of Thrones Cheezburger Site chzb.gr/LBpFrw 4 days ago
I Am Not Myself
Bills.Pay(Developer.Skills).ShouldBeTrue()
Sinister Bug is Sinister
Posted by on January 24, 2012
Can you spot the bug in this flags enumeration?
[Flags]
public enum ItemCoreChangeType
{
ViewStyleChanged = 1,
VaryByStructureChanged = 2,
LayersChanged = 4,
LayerContentsChanged = 8,
PropertiesChanged = 16,
ErrorsChanged = 32,
ChangeHistoryCleared = 64,
EditableCommentChanged = 128,
NeedsValidation = 256,
AffectsPropertyChangeHistory = 512,
AffectsLayerChangeHistory = 1024,
PushFormatters = 2048,
ResetValueGroups = 4096,
SkipChangeTrackingUpdate = 8092,
StatusChanged = 16384,
Flexible2DIndicesChanged = 32768,
}
In case you can’t spot it with the naked eye (I couldn’t, coworker Alex caught it.), watch this video. Pay attention to the binary display as I enter in the values of my enumeration above. Notice how the bit is steadily walking down the display? What happens when I hit SkipChangeTrackingUpdate?
So it seems that when I use the SkipChangeTrackingUpdate value I am actually sending many of the other values in this flags enumeration. Nasty bug to track down eh?
What makes this bug so sinister is just how long it took to find. Using the blame command in git, I see the following result. The names have been changed to protect the guilty.
72fe38b4 (dirk.diggler 2010-02-10 23:49:28 -0800 33) SkipChangeTrackingUpdate = 8092,
So this particular flaw has been in the code base unnoticed for close to two years. Two years worth of code has been written with the assumption that SkipChangeTrackingUpdate actually sends the value of 9 different flag values. Fixing the flag value could potentially effect any part of that code.
What do?
Lucky for me, a search for that flag in the entire code base returned 3 usages, one of which was the definition of the flag, one was the usage of the flag and one consuming the flag.
Crisis averted but it could have been much worse.
That is a tough one to spot. I do my flag enums like this, that way I don’t have to input the values myself and I know none of them will overlap:
[Flags]
public enum ItemCoreChangeType {
ViewStyleChanged = 1 << 0,
VaryByStructureChanged = 1 << 1,
LayersChanged = 1 << 2
…
}
Hey nice trick! I’ll have to remember that.
That is a good trick.
And I have to, shamefully, admit that I still didn’t see the problem even when you mentioned which enum item it was – only when i watched the video – then looked back that the value on that item did i finally see it.