]> Sergey Matveev's repositories - nnn.git/commitdiff
Allocate PATH_MAX aligned memory size
authorArun Prakash Jana <engineerarun@gmail.com>
Thu, 15 Jul 2021 18:52:27 +0000 (00:22 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Thu, 15 Jul 2021 19:49:09 +0000 (01:19 +0530)
src/nnn.c

index b0ab729d22af708ebf3560a80020f5498e132d0c..499073e5955ac4a32351e7a9f6e244d4afb5a47c 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
 #define ISBLANK(x)      ((x) == ' ' || (x) == '\t')
 #define TOUPPER(ch)     (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
 #define CMD_LEN_MAX     (PATH_MAX + ((NAME_MAX + 1) << 1))
+#define ALIGN_UP(x, A)  ((((x) + (A) - 1) / (A)) * (A))
 #define READLINE_MAX    256
 #define FILTER          '/'
 #define RFILTER         '\\'
@@ -1663,8 +1664,9 @@ static void invertselbuf(const int pathlen)
        /* Buffer size adjustment */
        selbufpos -= shrinklen;
 
-       if (alloclen > shrinklen) {
-               pselbuf = xrealloc(pselbuf, selbuflen + (alloclen - shrinklen));
+       if (alloclen > shrinklen && ((selbufpos + (alloclen - shrinklen)) > selbuflen)) {
+               selbuflen = ALIGN_UP(selbufpos + (alloclen - shrinklen), PATH_MAX);
+               pselbuf = xrealloc(pselbuf, selbuflen);
                if (!pselbuf)
                        errexit();
        }
@@ -1715,9 +1717,12 @@ static void addtoselbuf(const int pathlen, int startid, int endid)
                        alloclen += pathlen + dentp->nlen;
        }
 
-       pselbuf = xrealloc(pselbuf, selbuflen + alloclen);
-       if (!pselbuf)
-               errexit();
+       if ((selbufpos + alloclen) > selbuflen) {
+               selbuflen = ALIGN_UP(selbufpos + alloclen, PATH_MAX);
+               pselbuf = xrealloc(pselbuf, selbuflen);
+               if (!pselbuf)
+                       errexit();
+       }
 
        for (int i = startid; i <= endid; ++i) {
                if (!(pdents[i].flags & FILE_SELECTED)) {