]> granicus.if.org Git - procps-ng/commitdiff
Handle out of memory conditions.
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 11 Jul 2015 19:28:47 +0000 (21:28 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 11 Jul 2015 19:28:47 +0000 (21:28 +0200)
malloc and realloc could return NULL when no memory is available.
The code doesn't handle errors, so use xmalloc/xrealloc instead.

While at it, sync alloclen's type with len's type, so both are ssize_t.

pwdx.c

diff --git a/pwdx.c b/pwdx.c
index 07bedfc8f61051543a25ee43adbc2baf42d50e52..3e0afca6b2d904dffc53fe8ef8763909a6c09285 100644 (file)
--- a/pwdx.c
+++ b/pwdx.c
@@ -65,7 +65,7 @@ int main(int argc, char *argv[])
 {
        int ch;
        int retval = 0, i;
-       int alloclen = 128;
+       ssize_t alloclen = 128;
        char *pathbuf;
 
        static const struct option longopts[] = {
@@ -99,7 +99,7 @@ int main(int argc, char *argv[])
        if (argc == 0)
                usage(stderr);
 
-       pathbuf = malloc(alloclen);
+       pathbuf = xmalloc(alloclen);
 
        for (i = 0; i < argc; i++) {
                char *s;
@@ -128,7 +128,7 @@ int main(int argc, char *argv[])
                 */
                while ((len = readlink(buf, pathbuf, alloclen)) == alloclen) {
                        alloclen *= 2;
-                       pathbuf = realloc(pathbuf, alloclen);
+                       pathbuf = xrealloc(pathbuf, alloclen);
                }
                free(buf);