Skip to content

Commit 9130397

Browse files
gialludjb-rwth
authored andcommitted
avoid out-of-bound access in mystrncpy()
1 parent acbf2d3 commit 9130397

File tree

1 file changed

+14
-4
lines changed
  • INCHI-1-SRC/INCHI_BASE/src

1 file changed

+14
-4
lines changed

INCHI-1-SRC/INCHI_BASE/src/util.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,12 +1719,22 @@ int mystrncpy(char *target, const char *source, unsigned maxlen)
17191719
return 0;
17201720
}
17211721

1722-
if ((p = (const char*)memchr(source, 0, maxlen))) /* djb-rwth: addressing LLVM warning */
1723-
{ /* maxlen does not include the found zero termination */
1724-
len = (int) (p - source);
1722+
/* Find actual source length first to limit memchr search */
1723+
unsigned source_len = (unsigned)strlen(source);
1724+
1725+
if (source_len < maxlen)
1726+
{
1727+
/* Source is shorter than maxlen, use actual source length */
1728+
len = source_len;
1729+
}
1730+
else if ((p = (const char*)memchr(source, 0, maxlen)))
1731+
{
1732+
/* maxlen does not include the found zero termination */
1733+
len = (unsigned)(p - source);
17251734
}
17261735
else
1727-
{ /* reduced length does not include one more byte for zero termination */
1736+
{
1737+
/* reduced length does not include one more byte for zero termination */
17281738
len = maxlen - 1;
17291739
}
17301740

0 commit comments

Comments
 (0)