nnn

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8af773eb0d9242272ae0db854305d71dafa09781
parent 8ad807d4a99d65d17e855bf4ced5990664630d11
Author: Arun Prakash Jana <engineerarun@gmail.com>
Date:   Fri, 16 Jul 2021 00:22:27 +0530

Allocate PATH_MAX aligned memory size

Diffstat:
Msrc/nnn.c | 15++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/nnn.c b/src/nnn.c @@ -163,6 +163,7 @@ #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)) {