diff --git a/ChangeLog b/ChangeLog
index 3725cd21227386ca2665dccb1debb8beed4ad386..e93f15c3bc8ad89e6544884367176eb6674ceeef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -23,6 +23,9 @@ CVS code
   intput_tab()
 	- Fix assumption that matches is null terminated (David 
 	  Benbennick).
+  load_history()
+	- Fix segfault on loading huge strings from history file 
+	  (David Benbennick).
 - nano.c:
   breakable()
 	- Fix incorrect return value on short lines (David Benbennick).
diff --git a/files.c b/files.c
index 40c25a7a45983dc720cc7e75cbb529056374bdda..9db26dee83691376dcc7434899f9d3cdc1a5894d 100644
--- a/files.c
+++ b/files.c
@@ -2896,7 +2896,7 @@ void load_history(void)
 	    buf = charalloc(1024);
 	    while (fgets(buf, 1023, hist) != 0) {
 		ptr = buf;
-		while (*ptr != '\n')
+		while (*ptr != '\n' && *ptr != '\0' && ptr < buf + 1023)
 		    ptr++;
 		*ptr = '\0';
 		if (strlen(buf))