<?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>148620</bug_id>
          
          <creation_ts>2015-08-30 14:40:28 -0700</creation_ts>
          <short_desc>Undefined behavior in SQLiteStatement::getColumnText</short_desc>
          <delta_ts>2015-12-03 02:44:32 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebCore Misc.</component>
          <version>Other</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</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="Michael Catanzaro">mcatanzaro</reporter>
          <assigned_to name="Michael Catanzaro">mcatanzaro</assigned_to>
          <cc>beidson</cc>
    
    <cc>cgarcia</cc>
    
    <cc>darin</cc>
    
    <cc>mcatanzaro</cc>
    
    <cc>mrobinson</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1122077</commentid>
    <comment_count>0</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-08-30 14:40:28 -0700</bug_when>
    <thetext>There is undefined behavior in SQLiteStatement::getColumnText. sqlite3_column_text16 can trigger an internal type conversion, which would invalidate the result of sqlite3_column_bytes16. Here they are called as two different parameters to the same function, so the order is undefined. But the correctness of this function relies on sqlite3_column_text16 being called before sqlite3_column_bytes16.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1122080</commentid>
    <comment_count>1</comment_count>
      <attachid>260253</attachid>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-08-30 14:58:01 -0700</bug_when>
    <thetext>Created attachment 260253
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1127397</commentid>
    <comment_count>2</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-09-19 11:05:48 -0700</bug_when>
    <thetext>Note: I checked the rest of this file to make sure this pattern doesn&apos;t occur elsewhere.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1145572</commentid>
    <comment_count>3</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-12-02 08:38:26 -0800</bug_when>
    <thetext>(Easy review here!)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1145642</commentid>
    <comment_count>4</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2015-12-02 11:30:50 -0800</bug_when>
    <thetext>(In reply to comment #3)
&gt; (Easy review here!)

This is such a subtle thing, I think it&apos;d be awesome if the theoretical issue could be proven with a test.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1145717</commentid>
    <comment_count>5</comment_count>
      <attachid>260253</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-12-02 13:58:33 -0800</bug_when>
    <thetext>Comment on attachment 260253
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260253&amp;action=review

&gt; Source/WebCore/ChangeLog:15
&gt; +        There is undefined behavior in SQLiteStatement::getColumnText. sqlite3_column_text16 can
&gt; +        trigger an internal type conversion, which would invalidate the result of
&gt; +        sqlite3_column_bytes16. Here they are called as two different parameters to the same
&gt; +        function, so the order of execution is undefined. But the correctness of this function
&gt; +        relies on sqlite3_column_text16 being called before sqlite3_column_bytes16. Otherwise, we&apos;ll
&gt; +        wind up either truncating the string we create if the size is too short, or else creating it
&gt; +        with random memory if it&apos;s too big. Fix this by calling the functions sequentially instead
&gt; +        of doing it all in one statement.

My reading of &lt;https://sqlite.org/c3ref/column_blob.html&gt; says this analysis is incorrect. Both sqlite3_column_text16 and sqlite3_column_bytes16 convert the string to UTF-16. It doesn’t matter which is called first.

It’s true that the page recommends calling sqlite3_column_text16 first and then sqlite3_column_bytes16, but if you read carefully you’ll see that it’s equally safe to call sqlite3_column_bytes16 first and then sqlite3_column_text16, as long as you don’t call other functions.

I think there is no bug here.

&gt; Source/WebCore/platform/sql/SQLiteStatement.cpp:346
&gt; +    // size of the result, so it must strictly preceed the call to sqlite3_column_bytes16.

Spelling error here: precede is misspelled as “preceed”

&gt; Source/WebCore/platform/sql/SQLiteStatement.cpp:349
&gt; +    int length = sqlite3_column_bytes16(m_statement, col) / sizeof(UChar);
&gt; +    return String(text, length);

I wouldn’t split these into two separate lines.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1145718</commentid>
    <comment_count>6</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-12-02 14:00:21 -0800</bug_when>
    <thetext>(In reply to comment #4)
&gt; This is such a subtle thing, I think it&apos;d be awesome if the theoretical
&gt; issue could be proven with a test.

I don’t think that would be practical, because the ordering of evaluation of function arguments is undefined and so it’s compiler-dependent. If there was a bug here, it still might be impossible to reproduce on any given compiler.

But as I said above, I don’t think there is a bug here.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1145909</commentid>
    <comment_count>7</comment_count>
    <who name="Michael Catanzaro">mcatanzaro</who>
    <bug_when>2015-12-03 02:44:32 -0800</bug_when>
    <thetext>I think you&apos;re right. Thanks for the thorough review!</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>260253</attachid>
            <date>2015-08-30 14:58:01 -0700</date>
            <delta_ts>2015-12-02 13:58:33 -0800</delta_ts>
            <desc>Patch</desc>
            <filename>bug-148620-20150830165745.patch</filename>
            <type>text/plain</type>
            <size>2535</size>
            <attacher name="Michael Catanzaro">mcatanzaro</attacher>
            
              <data encoding="base64">U3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W5ndGggPSBzcWxpdGUzX2Nv
bHVtbl9ieXRlczE2KG1fc3RhdGVtZW50LCBjb2wpIC8gc2l6ZW9mKFVDaGFyKTsKKyAgICByZXR1
cm4gU3RyaW5nKHRleHQsIGxlbmd0aCk7CiB9CiAgICAgCiBkb3VibGUgU1FMaXRlU3RhdGVtZW50
OjpnZXRDb2x1bW5Eb3VibGUoaW50IGNvbCkK
</data>
<flag name="review"
          id="285442"
          type_id="1"
          status="-"
          setter="darin"
    />
          </attachment>
      

    </bug>

</bugzilla>