diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result
index 78d86d3f34220250c365950ab509f7583d28228c..6f6edd5112b75c32b6be742226f7e3722c59ec8e 100644
--- a/mysql-test/r/func_str.result
+++ b/mysql-test/r/func_str.result
@@ -722,9 +722,9 @@ Warning	1265	Data truncated for column 'format(130,10)' at row 1
 show create table t1;
 Table	Create Table
 t1	CREATE TABLE `t1` (
-  `bin(130)` varchar(64) NOT NULL DEFAULT '',
-  `oct(130)` varchar(64) NOT NULL DEFAULT '',
-  `conv(130,16,10)` varchar(64) NOT NULL DEFAULT '',
+  `bin(130)` varchar(64) DEFAULT NULL,
+  `oct(130)` varchar(64) DEFAULT NULL,
+  `conv(130,16,10)` varchar(64) DEFAULT NULL,
   `hex(130)` varchar(6) NOT NULL DEFAULT '',
   `char(130)` varbinary(4) NOT NULL DEFAULT '',
   `format(130,10)` varchar(4) NOT NULL DEFAULT '',
@@ -1315,6 +1315,18 @@ id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	SIMPLE	t1	system	NULL	NULL	NULL	NULL	0	0.00	const row not found
 Warnings:
 Note	1003	select decode('',_latin1'zxcv') AS `enc` from `test`.`t1`
+drop table t1;
+create table t1 (a bigint not null)engine=myisam;
+insert into t1 set a = 1024*1024*1024*4;
+delete from t1 order by (inet_ntoa(a)) desc limit 10;
+drop table t1;
+create table t1 (a char(36) not null)engine=myisam;
+insert ignore into t1 set a = ' ';
+insert ignore into t1 set a = ' ';
+select * from t1 order by (oct(a));
+a
+
+
 drop table t1;
 End of 4.1 tests
 create table t1 (d decimal default null);
diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test
index 0ba0ba30cd2a108d413e23fd5fe88a2d7c60d988..b6da14211aeee2e1400a1bad113eeba52680d50b 100644
--- a/mysql-test/t/func_str.test
+++ b/mysql-test/t/func_str.test
@@ -782,6 +782,19 @@ explain extended select encode(f1,'zxcv') as 'enc' from t1;
 explain extended select decode(f1,'zxcv') as 'enc' from t1;
 drop table t1;
 
+#
+# Bug #31758 inet_ntoa, oct, crashes server with null + filesort 
+#
+create table t1 (a bigint not null)engine=myisam;
+insert into t1 set a = 1024*1024*1024*4;
+delete from t1 order by (inet_ntoa(a)) desc limit 10;
+drop table t1;
+create table t1 (a char(36) not null)engine=myisam;
+insert ignore into t1 set a = ' ';
+insert ignore into t1 set a = ' ';
+select * from t1 order by (oct(a));
+drop table t1;
+
 --echo End of 4.1 tests
 
 #
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index 1a264fa2c01a826fe02900dced09a861a3052603..3608568bd5ee337b4fec7feeed99a823049f3754 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -558,7 +558,8 @@ public:
   void fix_length_and_dec()
   {
     collation.set(default_charset());
-    max_length= 64;
+    max_length=64;
+    maybe_null= 1;
   }
 };
 
@@ -656,7 +657,7 @@ public:
     }
   String* val_str(String* str);
   const char *func_name() const { return "inet_ntoa"; }
-  void fix_length_and_dec() { decimals = 0; max_length=3*8+7; }
+  void fix_length_and_dec() { decimals = 0; max_length=3*8+7; maybe_null=1;}
 };
 
 class Item_func_quote :public Item_str_func