Skip to content

Use _malloca() for debug strings to avoid buffer overflows#84

Merged
Liryna merged 1 commit intodokan-dev:masterfrom
nmlgc:master
Oct 10, 2015
Merged

Use _malloca() for debug strings to avoid buffer overflows#84
Liryna merged 1 commit intodokan-dev:masterfrom
nmlgc:master

Conversation

@nmlgc
Copy link
Contributor

@nmlgc nmlgc commented Oct 10, 2015

While the sprintf_s functions are supposed to protect from buffer overflows and other errors, they still raise an exception if the buffer is too small, so any debug string longer than 512 characters still crashes the program. In practice, this limit is exceeded very easily, for example, when matching a file name with a length of MAX_PATH against itself.

Instead of just increasing the buffer size and/or using snprintf, I prefer to use a combination of _vscwprintf() and _malloca()/_freea(), which computes the final length and then calls either alloca() for strings below 1024 bytes or malloc() for longer ones. This makes sure the string always gets shown in full (well, except for the unlikely case of a malloc() failure) and should eliminate this bug class without impacting performance too much.

While the sprintf_s functions are supposed to protect from buffer overflows and
other errors, they still raise an exception if the buffer is too small, so any
debug string longer than 512 characters still crashes the program. In practice,
this limit is exceeded very easily, for example, when matching a file name
with a length of MAX_PATH against itself.

Instead of just increasing the buffer size and/or using snprintf, I prefer to
use a combination of _vscwprintf() and _malloca()/_freea(), which computes the
final length and then calls either alloca() for strings below 1024 bytes or
malloc() for longer ones. This makes sure the string always gets shown in full
(well, except for the unlikely case of a malloc() failure) and should eliminate
this bug class without impacting performance too much.
@Liryna
Copy link
Member

Liryna commented Oct 10, 2015

Thank you !
This is perfectly working !
Always glad to see such contribution and special when it is about helping dokan stability and safety !

Liryna added a commit that referenced this pull request Oct 10, 2015
Use _malloca() for debug strings to avoid buffer overflows
@Liryna Liryna merged commit 7579d97 into dokan-dev:master Oct 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants