How to Minimum Use
Include denullpo.h in each C/C++ source, but it needs settings, otherwise all macros in denullpo.h become invalid.
All setting is written in "#define" before including denullpo.h.
- DENULLPO_TRAP
- If this is defined, error detection callback becomes effective.
- DENULLPO_SKIP
- If this is defined, Denullpo detects errors.
- If neither DENULLPO_TRAP nor DENULLPO_SKIP is defined, Denullpo does not detect errors.
- DENULLPO_4CC
- It is a value to indicate that an instance is valid.
- If this is not defined, it is defined at a constant value automatically.
At the spot that should check a pointer, give it in a pointer in
denullpo().
When a pointer is NULL, Denullpo processes a set error.
Sample Code
/*=== evade access to an NULL pointer with denullpo() ===*/
#include <stdio.h>
/*=== Denullpo needs setting before including denullpo.h ===*/
/*=== You can remove handling of Denullpo when you change this line into comment. ===*/
#define DENULLPO_SKIP
#include "denullpo.h"
int main(int argc,char *argv[]){
void* p=NULL;
int err=0;
p=malloc(128);
/*=== divides processing by a check result of a pointer ===*/
if(denullpo(p)){
/*=== processing when it is abnormal ===*/
fprintf(stderr,"this is nullpo\n");
err=1;
}
else{
/*=== processing when it is normal ===*/
// :
// :
// :
free(p);
p=NULL;
}
return err;
}
It examines the validity of instance
Denullpo can examine the validity of instance by adding a member for a magic code to a class and a structure body.
Class and structure of a check target define a writing point of a magic code in DENULLPO_INITCHECKER.
When created it in instance, it indicates that valid instance by calling
denullpo_setinit().
And when destroy it in instance, it indicates that invalid instance by calling
denullpo_setpurge().
At the spot that should check a pointer, give it in a pointer in
denullpo2().
When a pointer is illegal, Denullpo processes a set error.
Sample Code
/*=== resist a method call of illicit instance ===*/
#include <stdio.h>
#define DENULLPO_SKIP
#include "denullpo.h"
class FOO{
public:
/*=== define a variable to fill in a magic cord in a class ===*/
DENULLPO_INITCHECKER;
FOO(){
/*=== fill in a magic cord indicating that it initialized ===*/
denullpo_setinit(this);
}
~FOO(){
/*=== fill in a magic cord indicating that it terminated ===*/
denullpo_setpurge(this);
}
void exec(){
/*=== treat it as an error if this method is called from except between denullpo_setinit() and denullpo_setpurge() ===*/
if(denullpo2())return;
// :
// :
// :
}
};
Error detection callback
denullpo() and
denullpo2() usually return a result immediately when they detected an error.
When you call
denullpo_settrap() and set callback, callback is called when Denullpo detected an error, and the result is reflected.
DENULLPO_TRAP must be defined before including a denullpo.h to validate callback.
Sample Code
/*=== implement error handling of a illicit pointer ===*/
#include <stdio.h>
/*=== setting to validate callback ===*/
#define DENULLPO_TRAP
#include "denullpo.h"
int err=0;
/*=== processing when Denullpo detected an error ===*/
static unsigned __cdecl hook(void *p,eDenullpoReason reason){
fprintf(stderr,"nullpo is hooked\n");
err=1;
/*=== returning value just becomes returning value of denullpo() ===*/
return 1;
}
int main(int argc,char *argv[]){
void* p=NULL;
/*=== setting of callback ===*/
denullpo_settrap(hook);
p=malloc(128);
/*=== If a pointer is NULL, Denullpo execute set callback. ===*/
if(!denullpo(p)){
/*=== processing when it is normal ===*/
// :
// :
// :
free(p);
p=NULL;
}
return err;
}
Details
Definitions
eDenullpoReason
- DENULLPO_NULL
- Examined pointer is NULL.
- DENULLPO_INVALID
- Examined instance is invalid.
Callback Types
Callback when error is detected.
Arguments
p | pointer of an examination target |
reason | basis of an error |
Returning Value
0 | This error factor was solved. |
1 | This error factor is still left. |
Functions
get an error detection callback.
Returning Value
set an error detection callback.
Arguments
trap | a function to treat as callback |
Returning Value
Notes
This function is not thread safe.
call the error detection callback.
Arguments
p | pointer of an examination target |
reason | basis of an error |
Returning Value
0 | This error factor was solved. |
1 | This error factor is still left. |
unsigned denullpo_getinit(any* p)
get a value of instance checker
Arguments
Returning Value
value of instance checker |
void denullpo_setinit(any* p)
notify instance checker that instance is initialized
Arguments
Notes
You must call this macro for each instance to become a target of
denullpo2().
void denullpo_setpurge(any* p)
notify instance checker that instance is terminated
Arguments
Notes
You must call this macro for each instance to become a target of
denullpo2().
unsigned denullpo_chkinit(any* p)
examine whether instance is initialized
Arguments
Returning Value
0 | not abnormality |
1 | abnormality |
Notes
You do not usually call this macro directly. You call
denullpo2() so that Denullpo examines instance.
int denullpo(any* p)
examine whether a pointer is NULL
Arguments
Returning Value
0 | NULL was not detected |
1 | NULL was detected |
Notes
The callback function is called in case of the error detection if set by a callback function in
denullpo_settrap().
int denullpo2(any* p)
examine whether an instance is invalid
Arguments
Returning Value
0 | not abnormality |
1 | abnormality |
Notes
The callback function is called in case of the error detection if set by a callback function in
denullpo_settrap().