Skip to content

Instantly share code, notes, and snippets.

@peff
Last active December 16, 2018 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peff/479bc025e646bc29238c2cb7c73b9463 to your computer and use it in GitHub Desktop.
Save peff/479bc025e646bc29238c2cb7c73b9463 to your computer and use it in GitHub Desktop.
Hacky perl script to grep for Microsoft's SDL-banned functions.
#!/usr/bin/perl
#
# Usage: git grep -E "$(./sdl banned)" -- '*.c' '*.h'
my @TABLE = (
# Table 1. Banned string copy functions and replacements
BANNED => [qw(
strcpy
strcpyA
strcpyW
wcscpy
_tcscpy
_mbscpy
StrCpy
StrCpyA
StrCpyW
lstrcpy
lstrcpyA
lstrcpyW
_tccpy
_mbccpy
_ftcscpy
strncpy
wcsncpy
_tcsncpy
_mbsncpy
_mbsnbcpy
StrCpyN
StrCpyNA
StrCpyNW
StrNCpy
strcpynA
StrNCpyA
StrNCpyW
lstrcpyn
lstrcpynA
lstrcpynW
)],
# Table 2. Banned string concatenation functions and replacements
BANNED => [qw(
strcat
strcatA
strcatW
wcscat
_tcscat
_mbscat
StrCat
StrCatA
StrCatW
lstrcat
lstrcatA
lstrcatW
StrCatBuff
StrCatBuffA
StrCatBuffW
StrCatChainW
_tccat
_mbccat
_ftcscat
strncat
wcsncat
_tcsncat
_mbsncat
_mbsnbcat
StrCatN
StrCatNA
StrCatNW
StrNCat
StrNCatA
StrNCatW
lstrncat
lstrcatnA
lstrcatnW
lstrcatn
)],
# Table 3. Banned sprintf functions and replacements
BANNED => [qw(
sprintfW
sprintfA
wsprintf
wsprintfW
wsprintfA
sprintf
swprintf
_stprintf
wvsprintf
wvsprintfA
wvsprintfW
vsprintf
_vstprintf
vswprintf
)],
RECOMMENDED => [qw(
wnsprintf
wnsprintfA
wnsprintfW
_snwprintf
snprintf
sntprintf _vsnprintf
vsnprintf
_vsnwprintf
_vsntprintf
wvnsprintf
wvnsprintfA
wvnsprintfW
)],
# Table 4. Banned "n" sprintf functions and replacements
RECOMMENDED => [qw(
_snwprintf
_snprintf
_sntprintf
nsprintf
)],
# Table 5. Banned variable argument sprintf functions and replacements
BANNED => [qw(
wvsprintf
wvsprintfA
wvsprintfW
vsprintf
_vstprintf
vswprintf
)],
# Table 6. Banned variable argument "n" sprintf functions and replacements
BANNED => [qw(
)],
RECOMMENDED => [qw(
_vsnprintf
_vsnwprintf
_vsntprintf
wvnsprintf
wvnsprintfA
wvnsprintfW
)],
# Table 7. Banned "n" string copy functions and replacements
BANNED => [qw(
strncpy
wcsncpy
_tcsncpy
_mbsncpy
_mbsnbcpy
StrCpyN
StrCpyNA
StrCpyNW
StrNCpy
strcpynA
StrNCpyA
StrNCpyW
lstrcpyn
lstrcpynA
lstrcpynW
_fstrncpy
)],
# Table 8. Banned "n" string concatenation functions and replacements
BANNED => [qw(
strncat
wcsncat
_tcsncat
_mbsncat
_mbsnbcat
StrCatN
StrCatNA
StrCatNW
StrNCat
StrNCatA
StrNCatW
lstrncat
lstrcatnA
lstrcatnW
lstrcatn
_fstrncat
)],
# Table 9. Banned string tokenizing functions and replacements
RECOMMENDED => [qw(
strtok
_tcstok
wcstok
_mbstok
)],
# Table 10. Banned Makepath functions and replacements
RECOMMENDED => [qw(
makepath
_tmakepath
_makepath
_wmakepath
)],
# Table 11. Banned Splitpath functions and replacements
RECOMMENDED => [qw(
_splitpath
_tsplitpath
_wsplitpath
)],
# Table 12. Banned scanf functions and replacements
RECOMMENDED => [qw(
scanf
wscanf
_tscanf
sscanf
swscanf
_stscanf
)],
# Table 13. Banned "n" scanf functions and replacements
RECOMMENDED => [qw(
snscanf
snwscanf
_sntscanf
)],
# Table 14. Banned numeric conversion functions and replacements
RECOMMENDED => [qw(
_itoa
_itow
_i64toa
_i64tow
_ui64toa
_ui64tot
_ui64tow
_ultoa
_ultot
_ultow
)],
# Table 15. Banned gets functions and replacements
BANNED => [qw(
gets
_getts
_gettws
)],
# Table 16. Banned IsBad* functions and replacements
BANNED => [qw(
IsBadWritePtr
IsBadHugeWritePtr
IsBadReadPtr
IsBadHugeReadPtr
IsBadCodePtr
IsBadStringPtr
)],
# Table 17. Banned OEM conversion functions and replacements
RECOMMENDED => [qw(
CharToOem
CharToOemA
CharToOemW
OemToChar
OemToCharA
OemToCharW
CharToOemBuffA
CharToOemBuffW
)],
# Table 18. Banned stack dynamic memory allocation functions and replacements
RECOMMENDED => [qw(
alloca
_alloca
)],
# Table 19. Banned string length functions and replacements
RECOMMENDED => [qw(
strlen
wcslen
_mbslen
_mbstrlen
StrLen
lstrlen
)],
# Table 20. Banned memory copy functions and replacements
BANNED => [qw(
memcpy
RtlCopyMemory
CopyMemory
wmemcpy
)],
# Table 21. Banned window messaging functions and replacements
RECOMMENDED => [qw(
ChangeWindowMessageFilter
)],
);
my $want_type = uc(shift);
my %ignore = map { $_ => 1 } @ARGV;
my @funcs;
while (@TABLE) {
my $type = shift @TABLE;
my $names = shift @TABLE;
next unless $type eq $want_type;
push @funcs, grep { !$ignore{$_} } @$names;
}
print '(^|[^a-zA-Z0-9_])(' . join('|', @funcs) . ') *\('
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment