• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision4172 (tree)
Time2021-10-02 12:28:23
Authormagicant

Log Message

Don't allocate empty region with realloc (#42837)

Change Summary

Incremental Difference

--- yash/trunk/NEWS (revision 4171)
+++ yash/trunk/NEWS (revision 4172)
@@ -10,6 +10,8 @@
1010 ----------------------------------------------------------------------
1111 Yash 2.52
1212
13+ * Fixed possible memory leak caused by improper use of the realloc
14+ function.
1315 . Updated completion scripts:
1416 * git: Fixed completion of arguments in a command line containing
1517 argument-taking options such as "-C".
--- yash/trunk/configure (revision 4171)
+++ yash/trunk/configure (revision 4172)
@@ -1,5 +1,5 @@
11 # Manually written configuration script for yash
2-# (C) 2007-2020 magicant
2+# (C) 2007-2021 magicant
33 #
44 # This program is free software: you can redistribute it and/or modify
55 # it under the terms of the GNU General Public License as published by
@@ -1356,6 +1356,7 @@
13561356 while (getcwd(pwd, pwdlen) == NULL) {
13571357 if (errno == ERANGE) {
13581358 pwdlen *= 2;
1359+ if (!pwdlen) return NULL;
13591360 pwd = realloc(pwd, pwdlen);
13601361 if (!pwd) return NULL;
13611362 } else {
--- yash/trunk/util.h (revision 4171)
+++ yash/trunk/util.h (revision 4172)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* util.h: miscellaneous utility functions */
3-/* (C) 2007-2012 magicant */
3+/* (C) 2007-2021 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -135,8 +135,20 @@
135135 /* Attempts `realloc' and aborts the program on failure. */
136136 void *xrealloc(void *ptr, size_t size)
137137 {
138+ /* The behavior of `realloc(non_null_pointer, 0)' is unreliable. Some
139+ * implementations free the previously allocated region and return NULL.
140+ * Some reallocate an empty region and return a pointer to it. The latter
141+ * may fail to allocate the new region, leaving the previous region intact
142+ * and returning NULL. That means, when `realloc(non_null_pointer, 0)'
143+ * returned NULL, we cannot tell if the previous region has been freed or
144+ * not. */
145+ if (size == 0) {
146+ free(ptr);
147+ return NULL;
148+ }
149+
138150 void *result = realloc(ptr, size);
139- if (result == NULL && size > 0)
151+ if (result == NULL)
140152 alloc_failed();
141153 return result;
142154 }
Show on old repository browser