diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result
index 0d4ce9414e573ccb359aec31678be2df8b5da801..f63860039d760e82354306d451dc4b50f73957e7 100644
--- a/mysql-test/r/func_time.result
+++ b/mysql-test/r/func_time.result
@@ -1375,4 +1375,10 @@ Warning	1292	Truncated incorrect time value: ''
 Warning	1292	Truncated incorrect time value: ''
 Warning	1292	Truncated incorrect time value: ''
 DROP TABLE t1;
+#
+# Bug#11766112  59151:UNINITIALIZED VALUES IN EXTRACT_DATE_TIME WITH STR_TO_DATE(SPACE(..) ...
+#
+SELECT STR_TO_DATE(SPACE(2),'1');
+STR_TO_DATE(SPACE(2),'1')
+0000-00-00
 End of 5.1 tests
diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test
index f32110ef87c2ab0455bcb8d2203f9d76883760f3..c48351d33f2892acca8d4b8ec61cb24b4cf708fd 100644
--- a/mysql-test/t/func_time.test
+++ b/mysql-test/t/func_time.test
@@ -881,4 +881,10 @@ INSERT INTO t1 VALUES (''),('');
 SELECT COUNT(*) FROM t1 GROUP BY TIME_TO_SEC(a);
 DROP TABLE t1;
 
+--echo #
+--echo # Bug#11766112  59151:UNINITIALIZED VALUES IN EXTRACT_DATE_TIME WITH STR_TO_DATE(SPACE(..) ...
+--echo #
+
+SELECT STR_TO_DATE(SPACE(2),'1');
+
 --echo End of 5.1 tests
diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index 6335199b8debc668df3f81eeb0bed17407e96c00..71b2baf4feeffae944585f51c02209bcbedda5bc 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -294,8 +294,8 @@ static bool extract_date_time(DATE_TIME_FORMAT *format,
   for (; ptr != end && val != val_end; ptr++)
   {
     /* Skip pre-space between each argument */
-    while (val != val_end && my_isspace(cs, *val))
-      val++;
+    if ((val+= cs->cset->scan(cs, val, val_end, MY_SEQ_SPACES)) >= val_end)
+      break;
 
     if (*ptr == '%' && ptr+1 != end)
     {