<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>160565</bug_id>
          
          <creation_ts>2016-08-04 13:45:46 -0700</creation_ts>
          <short_desc>Undefined behavior in StdLibExtras.h, bitCount</short_desc>
          <delta_ts>2016-08-05 10:02:49 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jonathan Bedard">jbedard</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>benjamin</cc>
    
    <cc>cdumez</cc>
    
    <cc>cmarcelo</cc>
    
    <cc>commit-queue</cc>
    
    <cc>dbates</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1217567</commentid>
    <comment_count>0</comment_count>
    <who name="Jonathan Bedard">jbedard</who>
    <bug_when>2016-08-04 13:45:46 -0700</bug_when>
    <thetext>In StdLibExtras.h bitCount, there is undefined behavior on return.  The line:
    return (((bits + (bits &gt;&gt; 4)) &amp; 0xF0F0F0F) * 0x1010101) &gt;&gt; 24
almost always has undefined behavior.  In this case, the overflow is expected.  However, compiler optimization (given that this function is an inline) with constants may preform unexpected operations</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1217600</commentid>
    <comment_count>1</comment_count>
      <attachid>285364</attachid>
    <who name="Jonathan Bedard">jbedard</who>
    <bug_when>2016-08-04 14:20:43 -0700</bug_when>
    <thetext>Created attachment 285364
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1217879</commentid>
    <comment_count>2</comment_count>
    <who name="Jonathan Bedard">jbedard</who>
    <bug_when>2016-08-05 09:05:57 -0700</bug_when>
    <thetext>Here is the error message clang&apos;s undefined behavior sanitizer emits for this particular error:

/Volumes/Data/Code/UndefinedBehavior/OpenSource/WebKitBuild/Debug/usr/local/include/wtf/StdLibExtras.h:163:48: runtime error: unsigned integer overflow: 1025 * 16843009 cannot be represented in type &apos;unsigned int&apos;

Further investigation on this has revealed that clang is combining some &apos;suspicious&apos; behaviors with &apos;undefined&apos; behaviors, and this is a case of &apos;suspicious&apos; behavior, but, as per C++ 2014 standard 3.9.1, 4, unsigned types overflow with modulo 2^n, which is our desired behavior in this case.  In short, the behavior corrected here is both defined and desired.

Undefined behavior sanitizer information: http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

C++ standard:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

No fix is needed.  Marking as resolved.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="1"
              ispatch="1"
              isprivate="0"
          >
            <attachid>285364</attachid>
            <date>2016-08-04 14:20:43 -0700</date>
            <delta_ts>2016-08-05 09:06:15 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-160565-20160804141918.patch</filename>
            <type>text/plain</type>
            <size>1681</size>
            <attacher name="Jonathan Bedard">jbedard</attacher>
            
              <data encoding="base64">SW5kZXg6IFNvdXJjZS9XVEYvQ2hhbmdlTG9nCj09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0KLS0tIFNvdXJjZS9XVEYvQ2hh
bmdlTG9nCShyZXZpc2lvbiA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09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09Ci0tLSBTb3VyY2UvV1RGL3d0Zi9TdGRMaWJFeHRyYXMuaAkocmV2aXNpb24g
MjA0MTA1KQorKysgU291cmNlL1dURi93dGYvU3RkTGliRXh0cmFzLmgJKHdvcmtpbmcgY29weSkK
QEAgLTE1OCw5ICsxNTgsOSBAQCBpbmxpbmUgVG9UeXBlIHNhZmVDYXN0KEZyb21UeXBlIHZhbHVl
KQogLy8gUmV0dXJucyBhIGNvdW50IG9mIHRoZSBudW1iZXIgb2YgYml0cyBzZXQgaW4gJ2JpdHMn
LgogaW5saW5lIHNpemVfdCBiaXRDb3VudCh1bnNpZ25lZCBiaXRzKQogewotICAgIGJpdHMgPSBi
aXRzIC0gKChiaXRzID4+IDEpICYgMHg1NTU1NTU1NSk7Ci0gICAgYml0cyA9IChiaXRzICYgMHgz
MzMzMzMzMykgKyAoKGJpdHMgPj4gMikgJiAweDMzMzMzMzMzKTsKLSAgICByZXR1cm4gKCgoYml0
cyArIChiaXRzID4+IDQpKSAmIDB4RjBGMEYwRikgKiAweDEwMTAxMDEpID4+IDI0OworICAgIHVp
bnQ2NF90IGJpZ0JpdHMgPSBiaXRzIC0gKChiaXRzID4+IDEpICYgMHg1NTU1NTU1NSk7CisgICAg
YmlnQml0cyA9IChiaWdCaXRzICYgMHgzMzMzMzMzMykgKyAoKGJpZ0JpdHMgPj4gMikgJiAweDMz
MzMzMzMzKTsKKyAgICByZXR1cm4gKCgoKGJpZ0JpdHMgKyAoYmlnQml0cyA+PiA0KSkgJiAweEYw
RjBGMEYpICogMHgxMDEwMTAxKSAmIDB4RkZGMDAwMDApID4+IDI0OwogfQogCiBpbmxpbmUgc2l6
ZV90IGJpdENvdW50KHVpbnQ2NF90IGJpdHMpCg==
</data>

          </attachment>
      

    </bug>

</bugzilla>