Skip to content

Commit

Permalink
quote_for_pmake: fix counter underrun resulting in segfault
Browse files Browse the repository at this point in the history
while (nbs--) { ... } ends with nbs == -1. Rather than a minimal fix,
introduce mempset() to make these kinds of errors less likely in the
future.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392815
Reported-by: <13579and24680@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
  • Loading branch information
H. Peter Anvin committed Nov 7, 2022
1 parent 7a2b5c9 commit 2d4e695
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 5 additions & 7 deletions asm/nasm.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2020 The NASM Authors - All Rights Reserved
* Copyright 1996-2022 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
Expand Down Expand Up @@ -817,8 +817,7 @@ static char *quote_for_pmake(const char *str)
}

/* Convert N backslashes at the end of filename to 2N backslashes */
if (nbs)
n += nbs;
n += nbs;

os = q = nasm_malloc(n);

Expand All @@ -827,10 +826,10 @@ static char *quote_for_pmake(const char *str)
switch (*p) {
case ' ':
case '\t':
while (nbs--)
*q++ = '\\';
q = mempset(q, '\\', nbs);
*q++ = '\\';
*q++ = *p;
nbs = 0;
break;
case '$':
*q++ = *p;
Expand All @@ -852,9 +851,8 @@ static char *quote_for_pmake(const char *str)
break;
}
}
while (nbs--)
*q++ = '\\';

q = mempset(q, '\\', nbs);
*q = '\0';

return os;
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ AC_CHECK_FUNCS(strrchrnul)
AC_CHECK_FUNCS(iscntrl)
AC_CHECK_FUNCS(isascii)
AC_CHECK_FUNCS(mempcpy)
AC_CHECK_FUNCS(mempset)

AC_CHECK_FUNCS(getuid)
AC_CHECK_FUNCS(getgid)
Expand Down
7 changes: 7 additions & 0 deletions include/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ static inline void *mempcpy(void *dst, const void *src, size_t n)
}
#endif

#ifndef HAVE_MEMPSET
static inline void *mempset(void *dst, int c, size_t n)
{
return (char *)memset(dst, c, n) + n;
}
#endif

/*
* Hack to support external-linkage inline functions
*/
Expand Down

0 comments on commit 2d4e695

Please sign in to comment.