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

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