Blame SOURCES/0001-check-stat-s-result-and-avoid-calling-stat-on-a-NULL.patch

6ab22e
From fc87b0a32c130a2b3ab37e614d4a1c6c8e5d70e7 Mon Sep 17 00:00:00 2001
6ab22e
From: Kamil Dudka <kdudka@redhat.com>
6ab22e
Date: Thu, 19 Aug 2010 13:58:12 +0200
6ab22e
Subject: [PATCH 1/2] check stat's result and avoid calling stat on a NULL pointer
6ab22e
6ab22e
---
6ab22e
 src/files.c |   33 +++++++++++++++++++++++++--------
6ab22e
 1 files changed, 25 insertions(+), 8 deletions(-)
6ab22e
6ab22e
diff --git a/src/files.c b/src/files.c
6ab22e
index f6efbf1..99cc1b8 100644
6ab22e
--- a/src/files.c
6ab22e
+++ b/src/files.c
6ab22e
@@ -103,6 +103,24 @@ void initialize_buffer_text(void)
6ab22e
     openfile->totsize = 0;
6ab22e
 }
6ab22e
 
6ab22e
+#ifndef NANO_TINY
6ab22e
+/* If *pstat is NULL, perform a stat call with the given file name.  On success,
6ab22e
+ * *pstat points to a newly allocated buffer that contains the stat's result.
6ab22e
+ * On stat's failure, the NULL pointer in *pstat is left intact. */
6ab22e
+void stat_if_needed(const char *filename, struct stat **pstat)
6ab22e
+{
6ab22e
+    struct stat *tmp;
6ab22e
+    if (*pstat)
6ab22e
+	return;
6ab22e
+
6ab22e
+    tmp = (struct stat *)nmalloc(sizeof(struct stat));
6ab22e
+    if (0 == stat(filename, tmp))
6ab22e
+	*pstat = tmp;
6ab22e
+    else
6ab22e
+	free(tmp);
6ab22e
+}
6ab22e
+#endif
6ab22e
+
6ab22e
 /* If it's not "", filename is a file to open.  We make a new buffer, if
6ab22e
  * necessary, and then open and read the file, if applicable. */
6ab22e
 void open_buffer(const char *filename, bool undoable)
6ab22e
@@ -148,11 +166,7 @@ void open_buffer(const char *filename, bool undoable)
6ab22e
     if (rc > 0) {
6ab22e
 	read_file(f, rc, filename, undoable, new_buffer);
6ab22e
 #ifndef NANO_TINY
6ab22e
-	if (openfile->current_stat == NULL) {
6ab22e
-	    openfile->current_stat =
6ab22e
-		(struct stat *)nmalloc(sizeof(struct stat));
6ab22e
-	    stat(filename, openfile->current_stat);
6ab22e
-	}
6ab22e
+	stat_if_needed(filename, &openfile->current_stat);
6ab22e
 #endif
6ab22e
     }
6ab22e
 
6ab22e
@@ -1532,8 +1546,8 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
6ab22e
      * specified it interactively), stat and save the value
6ab22e
      * or else we will chase null pointers when we do
6ab22e
      * modtime checks, preserve file times, etc. during backup */
6ab22e
-    if (openfile->current_stat == NULL && !tmp && realexists)
6ab22e
-	stat(realname, openfile->current_stat);
6ab22e
+    if (!tmp && realexists)
6ab22e
+	stat_if_needed(realname, &openfile->current_stat);
6ab22e
 
6ab22e
     /* We backup only if the backup toggle is set, the file isn't
6ab22e
      * temporary, and the file already exists.  Furthermore, if we
6ab22e
@@ -1924,7 +1938,10 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
6ab22e
 	if (openfile->current_stat == NULL)
6ab22e
 	    openfile->current_stat =
6ab22e
 		(struct stat *)nmalloc(sizeof(struct stat));
6ab22e
-	stat(realname, openfile->current_stat);
6ab22e
+	if (stat(realname, openfile->current_stat)) {
6ab22e
+	    free(openfile->current_stat);
6ab22e
+	    openfile->current_stat = NULL;
6ab22e
+	}
6ab22e
 #endif
6ab22e
 
6ab22e
 	statusbar(P_("Wrote %lu line", "Wrote %lu lines",
6ab22e
-- 
6ab22e
1.7.4
6ab22e