From 88b4fd8eff93ffeece74f8c9cfdcce9b6661cea6 Mon Sep 17 00:00:00 2001
From: David Gow <davidgow@google.com>
Date: Fri, 24 Apr 2015 20:31:24 +0000
Subject: [PATCH] Stop mysql client dying if curses cannot start.

If setupterm() fails (which will occur if the TERM environment variable does
not have a known value), it will terminate the program. Here we check the
return value of setupterm() and fall back to a non-curses path. Thus, if TERM
is incorrectly set, the user will just miss out on bold text in the terminal,
rather than having the client die with a huge number of memory leak
stacktraces.

Note that:
- This bug seems to only affect MariaDB: Oracle's MySQL does not try to use
  curses at all.
- setupterm() will terminate the calling program on error if its errret
  argument is NULL. This is why we have a dummy variable to get the type of
  error.
---
 client/mysql.cc | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/client/mysql.cc b/client/mysql.cc
index ea7d17a..c176b61 100644
--- a/client/mysql.cc
+++ b/client/mysql.cc
@@ -4869,7 +4869,8 @@ static int
 put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
 {
   FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
-  static int inited=0;
+  static int inited=0, has_curses=0;
+  int curses_error;
 
   if (status.batch)
   {
@@ -4907,7 +4908,10 @@ put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
     {
       inited=1;
 #ifdef HAVE_SETUPTERM
-      (void) setupterm((char *)0, 1, (int *) 0);
+      if (setupterm((char *)0, 1, &curses_error) != ERR)
+      {
+        has_curses = 1;
+      }
 #endif
     }
     if (info_type == INFO_ERROR)
@@ -4920,7 +4924,8 @@ put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
         putchar('\a');		      	/* This should make a bell */
 #endif
       }
-      vidattr(A_STANDOUT);
+      if (has_curses)
+        vidattr(A_STANDOUT);
       if (error)
       {
 	if (sqlstate)
@@ -4938,10 +4943,11 @@ put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
       }
       tee_fputs(": ", file);
     }
-    else
+    else if (has_curses)
       vidattr(A_BOLD);
     (void) tee_puts(str, file);
-    vidattr(A_NORMAL);
+    if (has_curses)
+      vidattr(A_NORMAL);
   }
   if (unbuffered)
     fflush(file);
-- 
2.8.0.rc3.226.g39d4020

