0001-check-stat-s-result-and-avoid-calling-stat-on-a-NULL.patch 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. From fc87b0a32c130a2b3ab37e614d4a1c6c8e5d70e7 Mon Sep 17 00:00:00 2001
  2. From: Kamil Dudka <kdudka@redhat.com>
  3. Date: Thu, 19 Aug 2010 13:58:12 +0200
  4. Subject: [PATCH 1/2] check stat's result and avoid calling stat on a NULL pointer
  5. ---
  6. src/files.c | 33 +++++++++++++++++++++++++--------
  7. 1 files changed, 25 insertions(+), 8 deletions(-)
  8. diff --git a/src/files.c b/src/files.c
  9. index f6efbf1..99cc1b8 100644
  10. --- a/src/files.c
  11. +++ b/src/files.c
  12. @@ -103,6 +103,24 @@ void initialize_buffer_text(void)
  13. openfile->totsize = 0;
  14. }
  15. +#ifndef NANO_TINY
  16. +/* If *pstat is NULL, perform a stat call with the given file name. On success,
  17. + * *pstat points to a newly allocated buffer that contains the stat's result.
  18. + * On stat's failure, the NULL pointer in *pstat is left intact. */
  19. +void stat_if_needed(const char *filename, struct stat **pstat)
  20. +{
  21. + struct stat *tmp;
  22. + if (*pstat)
  23. + return;
  24. +
  25. + tmp = (struct stat *)nmalloc(sizeof(struct stat));
  26. + if (0 == stat(filename, tmp))
  27. + *pstat = tmp;
  28. + else
  29. + free(tmp);
  30. +}
  31. +#endif
  32. +
  33. /* If it's not "", filename is a file to open. We make a new buffer, if
  34. * necessary, and then open and read the file, if applicable. */
  35. void open_buffer(const char *filename, bool undoable)
  36. @@ -148,11 +166,7 @@ void open_buffer(const char *filename, bool undoable)
  37. if (rc > 0) {
  38. read_file(f, rc, filename, undoable, new_buffer);
  39. #ifndef NANO_TINY
  40. - if (openfile->current_stat == NULL) {
  41. - openfile->current_stat =
  42. - (struct stat *)nmalloc(sizeof(struct stat));
  43. - stat(filename, openfile->current_stat);
  44. - }
  45. + stat_if_needed(filename, &openfile->current_stat);
  46. #endif
  47. }
  48. @@ -1532,8 +1546,8 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
  49. * specified it interactively), stat and save the value
  50. * or else we will chase null pointers when we do
  51. * modtime checks, preserve file times, etc. during backup */
  52. - if (openfile->current_stat == NULL && !tmp && realexists)
  53. - stat(realname, openfile->current_stat);
  54. + if (!tmp && realexists)
  55. + stat_if_needed(realname, &openfile->current_stat);
  56. /* We backup only if the backup toggle is set, the file isn't
  57. * temporary, and the file already exists. Furthermore, if we
  58. @@ -1924,7 +1938,10 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type
  59. if (openfile->current_stat == NULL)
  60. openfile->current_stat =
  61. (struct stat *)nmalloc(sizeof(struct stat));
  62. - stat(realname, openfile->current_stat);
  63. + if (stat(realname, openfile->current_stat)) {
  64. + free(openfile->current_stat);
  65. + openfile->current_stat = NULL;
  66. + }
  67. #endif
  68. statusbar(P_("Wrote %lu line", "Wrote %lu lines",
  69. --
  70. 1.7.4