=item To ignore certain files:
perl typo.pl -ignore:[,[,...]]
ignores filenames that match the given patterns
'*' is only valid wilcard character
=for html

=item To scan files that have a modification date/time later than given
perl typo.pl -newer:,, ,,
scans files that have a mod. date/time later than
given, i.e. -newer:1998,1,31,4,30 => Jan 31, 1998 04:30
=for html

=item To print out the time when the script starts and stops scanning
perl typo.pl -showtime
=item To print out the # of comments and which functions were seen
perl typo.pl -showstats
=item To print out the file that the script is currently processing
perl typo.pl -showprogress
=item To enable nonbuffered output
perl typo.pl -nonbuffered
=item To enable line-by-line disabling of specific cases
perl typo.pl -notypo
Code must be annotated with the word "NO_TYPO"
on the line to be disabled for typo reporting.
To specify certain cases to be disabled, use
the format S where XX, YY
represent the specific cases that are disabled.
=for html

=item To check the results of additional functions
perl typo.pl -cfr:[,[,...]]
scans results of specified functions to see
if they are used before they have been checked for success
=for html

=item To use a text file to specify a list of options
perl typo.pl -optionfile:
reads in all the lines of the specified file and parses
each line as a possible option
=for html

=item To specify directories to look for option files
perl typo.pl -optiondir:[,[,...]]
specifies directories to be searched if the option file
cannot be found in the current directory. This option should
be specified before -optionfile. If directories are not
specified with trailing backslashes, then backslashes
will be appended.
=for html

=item To add to list that checks on function results
perl typo.pl -checked:[,[,...]]
informs script that the specified functions will
check the result from previous function calls
so there's no need to report use before check typo
i.e. if you specify C, then
x = malloc(16);
foobar(x);
won't report a typo.
=for html

=item To add to list that doesn't deref/access function results
perl typo.pl -noderef:[,[,...]]
informs script that the specified functions will
not dereference the result from previous function calls
so there's no need to report use before check typo yet.
i.e. if you specify C, then
x = malloc(16);
Output("%8.8lX\n", x);
won't report a typo yet.
=for html

=item To add to list that checks on function results similar to operator new
perl typo.pl -new:[,[,...]]
informs script that the specified functions behave
similar to C for case 34.
=for html

=item To check the results of functions that return handles
perl typo.pl -fn:HANDLE:[,[,...]]
scans results of specified functions to see
if they're used before they've been checked
for success and if the result is compared to
C.
=for html

=item To check the results of functions that return HRESULTs
perl typo.pl -fn:HR:[,[,...]]
scans results of specified functions to see
if they're used before they've been checked for success
and if the result is tested via
C or C macros.
=for html

=item To add to the function list that return the length of strings
perl typo.pl -fn:LEN:[,[,...]]
scans for use of specified functions that are used for
finding the lengths of null-terminated strings
=for html

=item To add to the function list that could overflow buffers
perl typo.pl -fn:OVERFLOW:[,[,...]]
scans for use of specified functions that can overflow
buffers passed to them
=for html

=item To add to the function list that behaves like C
perl typo.pl -fn:REALLOC:[,[,...]]
scans for use of specified functions that behave
like realloc (case 17)
=for html

=item To add to the function list that are registry functions
perl typo.pl -fn:REG:[,[,...]]
specifies registry functions whose result need to checked with ERROR_SUCCESS
=for html

=item To add to the function list that are RPC functions
perl typo.pl -fn:RPC:[,[,...]]
specifies RPC functions that need to be checked with RPC_* error codes
=for html

=item To add to the function list that are ignorable asserts
perl typo.pl -fn:SAFEASSERT:[,[,...]]
specifies assert functions that are ignored for case 4
=item To add to the function list that can throw/raise exceptions
perl typo.pl -fn:THROW:[,[,...]]
scans for specified functions to see if they're used in a C
=for html

=item To add to the function list that don't return a value
perl typo.pl -fn:VOID:[,[,...]]
prevents reports of case 32 for functions that don't
return a value.
=for html

=item To add to the function list that terminate a case statement
perl typo.pl -endcase:[,[,...]]
informs script that the specified functions behave
similar to C statement for case 19.
=for html

=item To add to list of #defines that may not be always defined
perl typo.pl -temp_defined:[,[,...]]
informs script that the specified defines may
not always be available.
=for html

=item To specify that C, C, C, C,
C, and C preprocessor directives should be handled
perl typo.pl -ifdef
There should be accompanying use of the C option B you want
the script to never check code within C's, C's, etc.
=for html

=item To specify symbols and their values for C, C, C, and C
preprocessor directives, use the C option:
perl typo.pl -define:[=]
This must be used with the C option
=for html

=item To print out help
perl typo.pl -help
perl typo.pl -h
perl typo.pl -?
=for html
=item To specify that results are output in XML format
perl typo.pl -output_xml
=for html
=item To specify the version of the typo.pl script required
perl typo.pl -version:
=for html
=item To specify functions whose results should be ignored
perl typo.pl -ignore_result:[,[,...]]
=item To specify that the code is run in kernel-mode and code in try-except blocks should B check for non-NULL ptrs
perl typo.pl -kernel_code
=for html
=item To specify that the script should only scan files that are in build.dat:
perl typo.pl -use_build_dat
The algorithm that the script uses to locate the build.dat is as follows:
=over 4
=item *
Check if the environment variable BASEDIR exists, if so, use that as the dir for build.dat
=item *
Otherwise check if the environment variables _NTROOT && _NTDRIVE exist, if so, use that as the dir for build.dat
If the dir is specified, check if build.dat is located in that dir.
=item *
If no build.dat was found, do the following scan, which works in non-build environment command windows
and for CE source trees:
=over 4
=item 1
Get current directory.
=item 2
If we find build.dat in current directory, use it.
=item 3
If we find a dirs or sources file, set parent dir as current dir (we don't actually change the current working dir).
=item 4
Go to step 2.
=back
=item *
If we still can't find a build.dat file, which may occur if we're in _WINCEROOT for a CE tree,
we search for build.dat in the current directory and any sub dirs. Any build.dat files
that are found are used.
=back
=back
=head1 ERRORS
Following potential programming errors are flagged:
=for html

bitwise-AND/OR/XOR of number compared to another value
may have undesired result due to C precedence rules since bitwise-AND/OR/XOR has lower precedence than
the comparison operators.
if (x & 1 == 0)
=for html

shift or mod operator ( <>, % ) followed by +,-,*,/ may
have undesired result due to C precedence rules.
The shift operator has lower precedence.
x = (y << 1 + 1);
is seen by the compiler as
x = y << (1 +1);
Some code confused the precedence of % and +/-.
% is higher than +/-.
=for html

assigning result of realloc to same var that's realloced
may result in leaked memory if realloc fails since C
will overwrite original value
p = (char *)realloc(p, 100);
=for html

=for html

18.

ReAlloc flags in wrong place or using ReAlloc flags for
a different realloc API,
pv1 = LocalReAlloc(pv, cbNew, GMEM_MOVEABLE);
i.e. passing C to C, it's not
an error to the compiler, but I'd say you were
playing with fire.
=for html

=for html

19.

C statement without a C/C/C/C
case 2:
Foo();
case 3:
Bar();
break;
If you add a comment with the text C or C
before the next case statement, then the script will not emit a warning.
=for html

C fails by raising an exception, so check to
see if C is within a C
BOOL foo(void)
{
PVOID pv;
pv = alloca(10);
return bar(pv);
}
=for html
N.B. You will only get one stack overflow exception.
The stack is left in an unstable state (the guard page at the
end of the stack has been converted to a normal stack page but
there is no room for a new guard page below it).
The next stack fault will walk off the bottom of the stack and the process
will be terminated immediately, no debugger, no nothing.
=for html

=for html

=for html

27.

check to see if the result from C or
C or some other specified function is checked at the first if-stmt.
hwnd = CreateWindow(...);
ShowWindow(hwnd, SW_SHOW);
N.B. I'd like to make this more flexible, as long as
the return value is checked before the value is used.
=for html

=for html

28.

check for multiple inequality comparisons of the same
var separated by "||",
i.e. C
in this case, if x == 0, the second comparison will
succeed and the code will enter the if-stmt body.
Programmer probably meant C instead of C.
=for html

=for html

29.

similar to 28, check for cases of the form:
if ((x == 0) && (x == 1))
=for html

=for html

30.

if a function result is used before it has
been checked for success
pv = LocalAlloc(...);
strcpy(pv, sz);
=for html

=for html

31.

check for use of C/C
strcpy(d, s);
=for html

=for html

32.

check to see if function result was stored somewhere
=for html

=for html

33.

trying to take the logical inverse of a number
x = !3;
=for html

=for html

34.

if the result from the C operator is used before
it has been checked for success
p = new CLASS;
p->DoIt();
=for html

=for html

35.

function that raises exception on error is not
in a C.
void foo()
{
InitializeCriticalSection(&crit);
.
.
.
}
=for html
From July 2000 MSDN, EnterCriticalSection:
=for html
In low memory situations, EnterCriticalSection can raise
a STATUS_INVALID_HANDLE exception.
To avoid problems, use structured exception handling, or call the
InitializeCriticalSectionAndSpinCount function to
preallocate the event used by EnterCriticalSection
instead of calling the InitializeCriticalSection function,
which forces EnterCriticalSection to allocate the event.
=for html
From July 2000 MSDN, InitializeCriticalSectionAndSpinCount:
=for html
Windows 2000: If the high-order bit is set, the function preallocates
the event used by the EnterCriticalSection function. Do not set this bit
if you are creating a large number of critical section objects,
because it will consume a significant amount of nonpaged pool.
=for html

=for html

36.

check for misspelled defined symbols. User must do
most of the investigative work. The script will
note all the symbols used in C,C,C,C
statements and print them out at the end.
=for html

check for use of C instead of C.
C is a pointer to the current object. C is the current object.
=for html

=for html

56.

check for use of C instead of C when calling
an API that wants number of characters in a buffer,
instead of number of bytes in a buffer
=for html

=for html

57.

check that results of registry APIs are compared with C or other common
registry error codes. C option was added to allow user to add to list
of registry APIs to check.
=for html

=for html

58.

check for use of a NULL DACL passed as the third parameter to C
=for html
From January 2001 MSDN, SetSecurityDescriptorDacl:
=for html
If this parameter is NULL, a NULL discretionary ACL is assigned to the security descriptor, B.
=for html

=for html

59.

check for non-NULL-ptr checks before calling memory deallocator function that can handle
NULL ptrs. These memory deallocators include C, C, C,
C, and C.
Since code performance usually follows the 80/20 rule (80% of the execution time is spent in 20% of the code),
case 59 may help reduce the size of the 80% of code that's not CPU-limited, i.e.
if (sz) LocalFree(sz);
The C check is extraneous since C can handle NULL ptrs.
If this was CPU-intensive code, you might not want to do a function call just for a NULL check.
B
Looking at some optimized code from VC, removing the extraneous check saves at least four bytes
if the ptr is in a register aleady!
=for html

=for html

60.

check for extremely long expressions [default=2048 chars]. This is usually a sign of code
that can be redesigned, either for maintainability or efficiency or both. Or the script's
parsing algorithm got confused...
=for html

=for html

61.

check for using C with C. C is a blocking call so if one
of the receiving windows is not responding, then your thread will block until the window responds.
You can work around this by using C, C, or only sending the windows message
to your known windows, if that's what you wanted to do.
=for html

=for html

62.

check for using C or C flags
with the C class of allocation APIs.
If the allocation fails, then the machine will crash.
=for html

=for html

=head1 MICROSOFT VISUAL STUDIO INTEGRATION
=over 4
=item 1
Select Tools.Customize menu item from MSDEV
=item 2
Click the Add button.
=item 3
Type "Typo" in the edit control and hit OK. Ignore the warning
about invalid path.
=item 4
In the Command edit control, enter perl.exe. Specify the
full pathname to perl.exe if it isn't on your PATH.
=item 5
In the Arguments edit control, enter the path to typo.pl.
=item 6
In the initial directory edit control, select one of the
directory entries from the dropdown menu, i.e. File Directory,
Current Directory, Target Directory, or Workspace Directory,
or enter a directory of your own.
=item 7
Make sure the "Redirect to Output Window" checkbox is set.
=item 8
Hit Close.
=back
You can now run TYPO.PL from Visual Studio and double-click on the
captured output to have it locate the potential error for you.
=head1 HISTORY
=over 4
=item Jan 06 1996
Created.
=for html

=item Jan 26 1996
Repeatedly replace parenthesized expressions (PE)
with '_' for errors 1 and 2.
Tweak C<==> case to remove PEs before checking
for unbalanced parentheses or lines that begin
with &&,||,==, or = * ==.
Tweak C<==> case to check previous line for
unterminated for-loop or line ending with
C or C.
=for html

=item Jan 29 1996
Adjust error 1 and 3 regexp so script
can find errors of the form C or
C
Print line number of error too
Released as Version 1.0.
=for html

=item Feb 08 1996
Suggestion:
Look for assignment within an C
Modify output so one can use typo.pl from Microsoft Visual Studio
to locate errors
Released as Version 1.0.1.
=for html

=item Apr 04 1996
Suggestion:
Look for pattern of the form C
Suggestion:
For assignment statements that use C<==>,
ignore previous lines which look like part of
C :> constructs or an assignment.
Check for C or C. Authour probably meant
to use bitwise versions.
Remove spaces from a copy of the current line to
make patterns simpler.
Allow user to specify list of files by piping
list of files to perl, i.e. C.
=for html