<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="956079">
  <date_last_updated>2012-05-03 06:18:04.116302+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/956079</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~wlad-montyprogram</bug_owner_link>
    <milestone_link></milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/956079/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/956079/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/956079</bug_web_link>
  <owner>Vladislav Vaintroub</owner>
  <assignee>Oleksandr &quot;Sanja&quot; Byelkin</assignee>
  <milestone_title></milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>Potential NULL pointer dereference in pagecache_fwrite</title>
  <status>Fix Released</status>
  <importance>High</importance>
  <created>2012-03-15 14:58:39.785207+00:00</created>
  <description>
<![CDATA[Inside pagecache_fwrite() , in ma_pagecache.c,  if my_pwrite() fails, the error handling callback function is called via function pointer.

the code in question is

  if (my_pwrite(filedesc->file, buffer, pagecache->block_size,
                ((my_off_t) pageno << pagecache->shift), flags))
  {
    (*filedesc->write_fail)(filedesc->callback_data);
    DBUG_RETURN(1);
  }


the problem is that  *filedesc->write_fail can be NULL, which means failing my_pwrite() will cause a crash by dereferencing NULL pointer.

To reproduce, 

1. add  line
DBUG_ASSERT(*filedesc->write_fail);
prior to code in question.

2. Compile with DBUG, run 
perl mysql-test-run.pl --suite=maria

3. Observe a crash . Here is for example what I get in 5.1

0028B9C3    mysqld.exe!my_sigabrt_handler()[mysqld.cc:2188]
00258DA9    mysqld.exe!raise()[winsig.c:586]
0025D4D6    mysqld.exe!abort()[abort.c:74]
008586A7    mysqld.exe!_wassert()[assert.c:336]
006351E1    mysqld.exe!pagecache_fwrite()[ma_pagecache.c:666]
0063B93D    mysqld.exe!flush_cached_blocks()[ma_pagecache.c:4370]
0063B1E1    mysqld.exe!flush_pagecache_blocks_int()[ma_pagecache.c:4660]
0063AAC7    mysqld.exe!flush_pagecache_blocks_with_filter()[ma_pagecache.c:4780]

0063CC23    mysqld.exe!_ma_once_end_block_record()[ma_blockrec.c:451]
005F9133    mysqld.exe!maria_close()[ma_close.c:81]
005D1A89    mysqld.exe!ha_maria::close()[ha_maria.cc:1018]
0040847A    mysqld.exe!closefrm()[table.cc:2013]
00369C11    mysqld.exe!intern_close_table()[sql_base.cc:793]]]>  </description>
  <activities>
    <activity datechanged="2012-03-15T14:58:39.785207+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Vladislav Vaintroub</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2012-03-16T15:08:00.388036+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[High]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2012-03-16T15:08:03.342498+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Oleksandr "Sanja" Byelkin (sanja-byelkin)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2012-03-16T15:08:08.272132+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[In Progress]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2012-03-19T12:29:42.046398+00:00">
      <oldvalue>
<![CDATA[In Progress]]>      </oldvalue>
      <newvalue>
<![CDATA[Fix Committed]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
    <activity datechanged="2012-05-03T06:18:02.933515+00:00">
      <oldvalue>
<![CDATA[Fix Committed]]>      </oldvalue>
      <newvalue>
<![CDATA[Fix Released]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Oleksandr &quot;Sanja&quot; Byelkin</person>
      <message></message>
    </activity>
  </activities>
  <comments>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/956079/comments/0" datecreated="2012-03-15T14:58:39.785207+00:00">
      <person>Vladislav Vaintroub</person>
      <subject>
<![CDATA[Potential NULL pointer dereference in pagecache_fwrite]]>      </subject>
      <content>
<![CDATA[Inside pagecache_fwrite() , in ma_pagecache.c,  if my_pwrite() fails, the error handling callback function is called via function pointer.

the code in question is

  if (my_pwrite(filedesc->file, buffer, pagecache->block_size,
                ((my_off_t) pageno << pagecache->shift), flags))
  {
    (*filedesc->write_fail)(filedesc->callback_data);
    DBUG_RETURN(1);
  }


the problem is that  *filedesc->write_fail can be NULL, which means failing my_pwrite() will cause a crash by dereferencing NULL pointer.

To reproduce, 

1. add  line
DBUG_ASSERT(*filedesc->write_fail);
prior to code in question.

2. Compile with DBUG, run 
perl mysql-test-run.pl --suite=maria

3. Observe a crash . Here is for example what I get in 5.1

0028B9C3    mysqld.exe!my_sigabrt_handler()[mysqld.cc:2188]
00258DA9    mysqld.exe!raise()[winsig.c:586]
0025D4D6    mysqld.exe!abort()[abort.c:74]
008586A7    mysqld.exe!_wassert()[assert.c:336]
006351E1    mysqld.exe!pagecache_fwrite()[ma_pagecache.c:666]
0063B93D    mysqld.exe!flush_cached_blocks()[ma_pagecache.c:4370]
0063B1E1    mysqld.exe!flush_pagecache_blocks_int()[ma_pagecache.c:4660]
0063AAC7    mysqld.exe!flush_pagecache_blocks_with_filter()[ma_pagecache.c:4780]

0063CC23    mysqld.exe!_ma_once_end_block_record()[ma_blockrec.c:451]
005F9133    mysqld.exe!maria_close()[ma_close.c:81]
005D1A89    mysqld.exe!ha_maria::close()[ha_maria.cc:1018]
0040847A    mysqld.exe!closefrm()[table.cc:2013]
00369C11    mysqld.exe!intern_close_table()[sql_base.cc:793]]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/956079/comments/1" datecreated="2012-03-15T15:00:53.580803+00:00">
      <person>Vladislav Vaintroub</person>
      <subject>
<![CDATA[Re: Potential NULL pointer dereference in pagecache_fwrite]]>      </subject>
      <content>
<![CDATA[My modification of source code :
=== modified file 'storage/maria/ma_pagecache.c'
--- storage/maria/ma_pagecache.c        2011-02-10 18:33:51 +0000
+++ storage/maria/ma_pagecache.c        2012-03-15 14:37:08 +0000
@@ -663,6 +663,7 @@
     DBUG_PRINT("error", ("write callback problem"));
     DBUG_RETURN(1);
   }
+  DBUG_ASSERT(*filedesc->write_fail);
   if (my_pwrite(filedesc->file, buffer, pagecache->block_size,
                 ((my_off_t) pageno << pagecache->shift), flags))
   {]]>      </content>
    </comment>
  </comments>
</launchpad-bug>
