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

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