From 5d71514f211c5b44a61870252c0df71373891657 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Wed, 29 Jan 2003 04:18:37 +0000
Subject: [PATCH] - Fix nano crashing when searching/replacing an invalid regex
 (try ^*).  Changed regexp_init() to return 1 or 0 based on regcomp()'s return
 value and search_init to exit with an error message (sorry Jordi)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1406 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog |  4 ++++
 proto.h   |  2 +-
 search.c  | 20 +++++++++++++++++---
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7c3afa0a..916f7f71 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -17,6 +17,10 @@ CVS Code -
 	  Affects files.c:load_open_file(), nano.c:main(), 
 	  search.c:findnextstr(), winio.c:statusbar() and 
 	  do_cursorpos() (David Benbennick).
+	- Fix nano crashing when searching/replacing an invalid
+	  regex (try "^*").  Changed regexp_init() to return 
+	  1 or 0 based on regcomp()'s return value and search_init 
+	  to exit with an error message (sorry Jordi!)
 - cut.c:
   do_cut_text()
 	- Fix incorrect cursor location when cutting long lines
diff --git a/proto.h b/proto.h
index 645e8226..8f202b56 100644
--- a/proto.h
+++ b/proto.h
@@ -334,7 +334,7 @@ void do_rcfile(void);
 
 /* Public functions in search.c */
 #ifdef HAVE_REGEX_H
-void regexp_init(const char *regexp);
+int regexp_init(const char *regexp);
 void regexp_cleanup(void);
 #endif
 void not_found_msg(const char *str);
diff --git a/search.c b/search.c
index 2966594c..5a625985 100644
--- a/search.c
+++ b/search.c
@@ -34,10 +34,15 @@
 /* Regular expression helper functions */
 
 #ifdef HAVE_REGEX_H
-void regexp_init(const char *regexp)
+int regexp_init(const char *regexp)
 {
-    regcomp(&search_regexp, regexp, (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE) | REG_EXTENDED);
+    /* Hmm, perhaps we should check for whether regcomp returns successfully */
+    if (regcomp(&search_regexp, regexp, (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE) 
+		| REG_EXTENDED) != 0)
+	return 0;
+
     SET(REGEXP_COMPILED);
+    return 1;
 }
 
 void regexp_cleanup(void)
@@ -165,7 +170,16 @@ int search_init(int replacing)
 	case 0:		/* They entered something new */
 #ifdef HAVE_REGEX_H
 	    if (ISSET(USE_REGEXP))
-		regexp_init(answer);
+		if (regexp_init(answer) == 0) {
+		    statusbar(_("Invalid regex!"));
+		    reset_cursor();
+		    free(backupstring);
+		    backupstring = NULL;
+#ifndef NANO_SMALL
+		    search_history.current = search_history.next;
+#endif
+		    return -3;
+		}
 #endif
 	    free(backupstring);
 	    backupstring = NULL;
-- 
GitLab