Skip to content

Commit cb568a8

Browse files
committed
[MERGE #5290 @xiaoyinl] Fix potential buffer overread in CountNewlines in Scan.cpp
Merge pull request #5290 from xiaoyinl:countnewlines `cch` parameter is ignored if `psz` is not null-terminated and `psz[cch-1] == '\r'` and `psz[cch] == '\n'`. For example, `CountNewlines(_u("ab\r\na\n\n"), 3)` should return 1 (only count the first \r), but it now returns 2 (the first \r\n is skipped but the trailing \n\n are counted). The problem is that the `break` at line 26 breaks the switch, and `cch` becomes -1. No callers actually pass `cch`, so removing it.
2 parents 99df6c9 + 590e7ce commit cb568a8

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

lib/Parser/Scan.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@
1010
* a given character can be part of an identifier, and so on.
1111
*/
1212

13-
int CountNewlines(LPCOLESTR psz, int cch)
13+
int CountNewlines(LPCOLESTR psz)
1414
{
1515
int cln = 0;
1616

17-
while (0 != *psz && 0 != cch--)
17+
while (0 != *psz)
1818
{
1919
switch (*psz++)
2020
{
2121
case _u('\xD'):
2222
if (*psz == _u('\xA'))
2323
{
2424
++psz;
25-
if (0 == cch--)
26-
break;
2725
}
2826
// fall-through
2927
case _u('\xA'):

lib/Parser/Scan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Js
1212
#include "Windows.Globalization.h"
1313
#endif
1414

15-
int CountNewlines(LPCOLESTR psz, int cch = -1);
15+
int CountNewlines(LPCOLESTR psz);
1616

1717
class Parser;
1818
struct ParseContext;

0 commit comments

Comments
 (0)