From aec43216c834c2156e9aca7b7dc182fbdd1916de Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Wed, 26 Oct 2016 21:38:58 +0000 Subject: MDEV-9409 Windows - workaround VS2015 CRT bug that makes mysqldump/mysql_install_db.exe fail The bug is described in https://connect.microsoft.com/VisualStudio/Feedback/Details/1902345 When reading from a pipe in text mode, using CRT function such as fread(), some newlines may be lost. Workaround is to use binary mode on reading side and if necessary, replace \r\n with \n. --- client/mysqltest.cc | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'client') diff --git a/client/mysqltest.cc b/client/mysqltest.cc index acb9e8b1e0c..5daa0e72270 100644 --- a/client/mysqltest.cc +++ b/client/mysqltest.cc @@ -1705,11 +1705,11 @@ int cat_file(DYNAMIC_STRING* ds, const char* filename) while((len= my_read(fd, (uchar*)&buff, sizeof(buff)-1, MYF(0))) > 0) { - char *p= buff, *start= buff; - while (p < buff+len) + char *p= buff, *start= buff,*end=buff+len; + while (p < end) { /* Convert cr/lf to lf */ - if (*p == '\r' && *(p+1) && *(p+1)== '\n') + if (*p == '\r' && p+1 < end && *(p+1)== '\n') { /* Add fake newline instead of cr and output the line */ *p= '\n'; @@ -3367,16 +3367,32 @@ void do_exec(struct st_command *command) ds_result= &ds_sorted; } +#ifdef _WIN32 + /* Workaround for CRT bug, MDEV-9409 */ + _setmode(fileno(res_file), O_BINARY); +#endif + while (fgets(buf, sizeof(buf), res_file)) { + int len = (int)strlen(buf); +#ifdef _WIN32 + /* Strip '\r' off newlines. */ + if (len > 1 && buf[len-2] == '\r' && buf[len-1] == '\n') + { + buf[len-2] = '\n'; + buf[len-1] = 0; + len--; + } +#endif if (disable_result_log) { - buf[strlen(buf)-1]=0; + if (len) + buf[len-1] = 0; DBUG_PRINT("exec_result",("%s", buf)); } else { - replace_dynstr_append(ds_result, buf); + replace_dynstr_append_mem(ds_result, buf, len); } } error= pclose(res_file); @@ -5200,7 +5216,7 @@ typedef struct static st_error global_error_names[] = { - { "", -1U, "" }, + { "", ~0U, "" }, #include { 0, 0, 0 } }; -- cgit v1.2.1