Tutorial 1 - Create a simple C/C++ Path checker
From Insight-9.5
Contents |
To get started with creating C/C++ Path checkers, you can build and deploy the sample C/C++ Path checker, called MallocDelete.
Run kwcreatechecker
In the directory where you want the checker files to be created, run kwcreatechecker with the following options:
kwcreatechecker --language cxx --type common --code MallocDelete
Note: Multibyte characters are not supported in error IDs (issue codes) and there's a 255-character limit.
The checker source files are created in the MallocDelete subdirectory.
Build the checker
Now we'll compile the checker and create a build specification file we can use to test it.
Windows
- Open the Visual Studio prompt.
- Go to your MallocDelete checker directory.
- Run:
-
nmake install
-
- To generate the build specification you’ll need to test your deployed checker, run:
-
nmake buildspec
-
Unix
- With GNU make in your path, run:
-
make install
-
- To generate the build specification you’ll need to test your deployed checker, run:
-
make buildspec
-
Deploy the checker
Now we’ll deploy the checker to our desktop so that it can be used by Klocwork Truepath™ to detect the new issue type.
If you do not see a plugins directory in the following location, you must create it manually. The location varies depending on your operating system:
- Unix: ~<username>/.klocwork/plugins
- Windows 7 and Vista: C:\users\<username>\.klocwork\plugins
- Windows XP: C:\Documents and Settings\<username>\.klocwork\plugins
Unzip the ZIP file into the plugins folder you created.
Test the checker
We’re going to use kwcheck, the command-line analysis tool, to make sure the new issue type is being detected in the test case.
- Set up the project to test. In your plugins folder, set up a local project:
-
kwcheck create -b <build-specification>
- where
- the create subcommand sets up local project directories you need to run an analysis
- -b imports the build specification you created when you compiled the checker
-
- Run kwcheck to see if the issue is detected in your test case:
-
kwcheck run
- Your checker will detect the issue from your test case.
-
If all is well, you should see something like this:
1 C:\MallocDelete\testcase.cc:5 FMM.MUST Analyze
Freeing mismatched memory from 'p'. Memory referenced by 'p' allocated through function 'malloc' at line 4 is illegally freed by passing argument 1 to function
'delete' at line 5
2 C:\MallocDelete\testcase.cc:5 MallocDelete Analyze
Your message goes here
The second detected issue was reported by the custom checker.
If you want to put your own message in the checker, edit the checkers.xml file and re-build the checker. For more information, see checkers.xml file: C/C++ checker configuration.
What’s next?
Try the more advanced tutorials:
- Tutorial 2 - Create an intraprocedural C/C++ Path checker
- Tutorial 3 - Create an interprocedural C/C++ Path checker
Documentation for custom C/C++ checkers
Overview
How-to
Tutorials
- KAST tutorials
- Path tutorials
- Tutorial 1 - Create a simple C/C++ Path checker
- Tutorial 2 - Create an intraprocedural C/C++ Path checker
- Tutorial 3 - Create an interprocedural C/C++ Path checker
Examples
Deployment
Reference


