Blame SOURCES/DBD-mysql-4.033_02-Fix-transferring-MYSQL_TYPE_LONG-values-on-64-bit-bi.patch

14b93c
From 604af7c0622261342929dd5087838af7d067976f Mon Sep 17 00:00:00 2001
14b93c
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
14b93c
Date: Fri, 26 Feb 2016 13:32:31 +0100
14b93c
Subject: [PATCH] Fix transferring MYSQL_TYPE_LONG values on 64-bit big endian
14b93c
 systems
14b93c
MIME-Version: 1.0
14b93c
Content-Type: text/plain; charset=UTF-8
14b93c
Content-Transfer-Encoding: 8bit
14b93c
14b93c
t/40server_prepare.t test failed on s390x platform. Server-prepared
14b93c
values of types int, smallint, and tinyint are passed to application
14b93c
as 32-bit integer. The same buffer was interpreted as long integer
14b93c
by DBD::MySQL. This caused missaligned read/write and bogus
14b93c
interpretation of the values.
14b93c
14b93c
https://rt.cpan.org/Public/Bug/Display.html?id=57266
14b93c
https://bugzilla.redhat.com/show_bug.cgi?id=1311646
14b93c
http://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-fetch.html
14b93c
Signed-off-by: Petr Písař <ppisar@redhat.com>
14b93c
Signed-off-by: Michiel Beijen <michiel.beijen@gmail.com>
14b93c
---
14b93c
 dbdimp.c | 20 +++++++++++++-------
14b93c
 dbdimp.h |  5 +++--
14b93c
 2 files changed, 16 insertions(+), 9 deletions(-)
14b93c
14b93c
diff --git a/dbdimp.c b/dbdimp.c
14b93c
index acdfee8..091df7d 100644
14b93c
--- a/dbdimp.c
14b93c
+++ b/dbdimp.c
14b93c
@@ -18,6 +18,7 @@
14b93c
 #endif
14b93c
 
14b93c
 #include "dbdimp.h"
14b93c
+#include <inttypes.h> /* for PRId32 */
14b93c
 
14b93c
 #if defined(WIN32)  &&  defined(WORD)
14b93c
 #undef WORD
14b93c
@@ -3752,8 +3753,8 @@ int dbd_describe(SV* sth, imp_sth_t* imp_sth)
14b93c
 
14b93c
       if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
14b93c
       {
14b93c
-        PerlIO_printf(DBIc_LOGPIO(imp_xxh),"\t\ti %d col_type %d fbh->length %d\n",
14b93c
-                      i, col_type, (int) fbh->length);
14b93c
+        PerlIO_printf(DBIc_LOGPIO(imp_xxh),"\t\ti %d col_type %d fbh->length %lu\n",
14b93c
+                      i, col_type, fbh->length);
14b93c
         PerlIO_printf(DBIc_LOGPIO(imp_xxh),
14b93c
                       "\t\tfields[i].length %lu fields[i].max_length %lu fields[i].type %d fields[i].charsetnr %d\n",
14b93c
                       (long unsigned int) fields[i].length, (long unsigned int) fields[i].max_length, fields[i].type,
14b93c
@@ -4014,8 +4015,8 @@ process:
14b93c
 
14b93c
         case MYSQL_TYPE_LONG:
14b93c
           if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
14b93c
-            PerlIO_printf(DBIc_LOGPIO(imp_xxh), "\t\tst_fetch int data %d, unsigned? %d\n",
14b93c
-                          (int) fbh->ldata, buffer->is_unsigned);
14b93c
+            PerlIO_printf(DBIc_LOGPIO(imp_xxh), "\t\tst_fetch int data %"PRId32", unsigned? %d\n",
14b93c
+                          fbh->ldata, buffer->is_unsigned);
14b93c
           if (buffer->is_unsigned)
14b93c
             sv_setuv(sv, fbh->ldata);
14b93c
           else
14b93c
@@ -4786,6 +4787,7 @@ int dbd_bind_ph(SV *sth, imp_sth_t *imp_sth, SV *param, SV *value,
14b93c
   int buffer_is_null= 0;
14b93c
   int buffer_length= slen;
14b93c
   unsigned int buffer_type= 0;
14b93c
+  IV tmp;
14b93c
 #endif
14b93c
 
14b93c
   D_imp_dbh_from_sth;
14b93c
@@ -4873,12 +4875,16 @@ int dbd_bind_ph(SV *sth, imp_sth_t *imp_sth, SV *param, SV *value,
14b93c
           if (!SvIOK(imp_sth->params[idx].value) && DBIc_TRACE_LEVEL(imp_xxh) >= 2)
14b93c
             PerlIO_printf(DBIc_LOGPIO(imp_xxh), "\t\tTRY TO BIND AN INT NUMBER\n");
14b93c
           buffer_length = sizeof imp_sth->fbind[idx].numeric_val.lval;
14b93c
-          imp_sth->fbind[idx].numeric_val.lval= SvIV(imp_sth->params[idx].value);
14b93c
+
14b93c
+          tmp = SvIV(imp_sth->params[idx].value);
14b93c
+          if (tmp > INT32_MAX)
14b93c
+	        croak("Could not bind %ld: Integer too large for MYSQL_TYPE_LONG", tmp);
14b93c
+          imp_sth->fbind[idx].numeric_val.lval= tmp;
14b93c
           buffer=(void*)&(imp_sth->fbind[idx].numeric_val.lval);
14b93c
           if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
14b93c
             PerlIO_printf(DBIc_LOGPIO(imp_xxh),
14b93c
-                          "   SCALAR type %d ->%ld<- IS A INT NUMBER\n",
14b93c
-                          (int) sql_type, (long) (*buffer));
14b93c
+                          "   SCALAR type %d ->%"PRId32"<- IS A INT NUMBER\n",
14b93c
+                          (int) sql_type, *(int32_t *)buffer);
14b93c
           break;
14b93c
 
14b93c
         case MYSQL_TYPE_DOUBLE:
14b93c
diff --git a/dbdimp.h b/dbdimp.h
14b93c
index 8723bcc..1ef5d72 100644
14b93c
--- a/dbdimp.h
14b93c
+++ b/dbdimp.h
14b93c
@@ -22,6 +22,7 @@
14b93c
 #include <mysqld_error.h>  /* Comes MySQL */
14b93c
 
14b93c
 #include <errmsg.h> /* Comes with MySQL-devel */
14b93c
+#include <stdint.h> /* For int32_t */
14b93c
 
14b93c
 /* For now, we hardcode this, but in the future,
14b93c
  * we can detect capabilities of the MySQL libraries
14b93c
@@ -212,7 +213,7 @@ typedef struct imp_sth_ph_st {
14b93c
 typedef struct imp_sth_phb_st {
14b93c
     union
14b93c
     {
14b93c
-      long lval;
14b93c
+      int32_t lval;
14b93c
       double dval;
14b93c
     } numeric_val;
14b93c
     unsigned long   length;
14b93c
@@ -233,7 +234,7 @@ typedef struct imp_sth_fbh_st {
14b93c
     char           *data;
14b93c
     int            charsetnr;
14b93c
     double         ddata;
14b93c
-    long           ldata;
14b93c
+    int32_t        ldata;
14b93c
 #if MYSQL_VERSION_ID < FIELD_CHARSETNR_VERSION 
14b93c
     unsigned int   flags;
14b93c
 #endif
14b93c
-- 
14b93c
2.7.4
14b93c