From: Adam Simpkins Date: Fri, 18 Apr 2014 18:53:32 +0000 (-0700) Subject: fix off-by-one error in IPAddress::longestCommonPrefix() X-Git-Tag: v0.22.0~586 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=dea4e16698c91af1fd112352400017048778fbfd;ds=sidebyside fix off-by-one error in IPAddress::longestCommonPrefix() Summary: Fix an off-by-one error causing an ASAN abort. When calling longestCommonPrefix() with a full mask, it would compare one byte past the end of the address data, and would end up writing past the end of the ba array on the stack. Test Plan: Built with ASAN, ran the unit tests, and verified the ASAN failure was gone. Reviewed By: jasmeetbagga@fb.com FB internal diff: D1284750 --- diff --git a/folly/detail/IPAddress.h b/folly/detail/IPAddress.h index 7f1095e0..476f1fa1 100644 --- a/folly/detail/IPAddress.h +++ b/folly/detail/IPAddress.h @@ -121,7 +121,7 @@ struct Bytes : private boost::noncopyable { // Compare a byte at a time. Note - I measured compared this with // going multiple bytes at a time (8, 4, 2 and 1). It turns out // to be 20 - 25% slower for 4 and 16 byte arrays. - while (byteIndex * 8 <= mask && one[byteIndex] == two[byteIndex]) { + while (byteIndex * 8 < mask && one[byteIndex] == two[byteIndex]) { ba[byteIndex] = one[byteIndex]; ++byteIndex; }