C/C++ Library, mos@ua 1.0
cpplib-mos
hdp

Handle Defensive Programming of C/C++ library functions module. More...

Files

file  hdp.h
 Handle Defensive Programming module.
 

Macros

#define MO(func_call)
 
#define Ne(func_call)
 
#define Nu(func_call)
 
#define NZ(func_call)
 
#define EF(func_call)
 
#define MOP(func_call)
 
#define errMO   MO
 
#define errNe   Ne
 
#define errNu   Nu
 
#define errNZ   NZ
 
#define errEF   EF
 
#define errMOP   MOP
 

Detailed Description

Handle Defensive Programming of C/C++ library functions module.

This file handles defensive programming approach of native C/C++ libraries functions, removing the responsibility of handling errors from the function's clients. Error identification uses errno values, although not necessarily using errno global variable (like in NZ macro, used for pthread library functions).

Errors are handled by two possible error handling policies (through the definition of a macro – EXIT_POLICY/EXCEPTION_POLICY – as a compiler option):

  1. EXIT_POLICY (default): describes the failed call in stderr (with the identification of the errno error, and the precise location the call), generates a segmentation fault (enabling a stack trace within a debugger like gdb), and exits program execution (applicable for C and C++);
  2. EXCEPTION_POLICY: throws a int exception with the (errno) status error returned by the original function (applicable only to C++).

To change the default (EXIT_POLICY) policy just add -DEXCEPTION_POLICY in the compiler options.

Available macros (name directly related with error test):

  1. MO/errMO [Minus One]: test error comparing to -1 (== -1) – error in errno
  2. Ne/errNe [Negative]: test error comparing to negative integer result (< 0) – error in errno
  3. Nu/errNu [Null]: test error comparing to NULL (== NULL) – error in errno
  4. NZ/errNZ [Not Zero]: test error comparing to non-zero value (!= 0) – error in function result
  5. EF/errEF [End of File]: test error comparing to EOF (== EOF) – error in errno
  6. MOP/errMOP [Minus One Pointer]: test error comparing to (void*)-1 (== (void*)-1) – error in errno

Examples:

pid_t ret = MO(fork());
MO(wait(NULL));
Ne(printf("Hello\n"));
int *p = (int*)Nu(malloc(10));
int shmid = MO(shmget(IPC_PRIVATE, sizeof(int), 0600 | IPC_CREAT | IPC_EXCL));
int* shm = (int*)MOP(shmat(shmid, NULL, 0));
NZ(pthread_create(...));
#define MO(func_call)
Definition hdp.h:133
#define Ne(func_call)
Definition hdp.h:153
#define MOP(func_call)
Definition hdp.h:233
#define NZ(func_call)
Definition hdp.h:193
#define Nu(func_call)
Definition hdp.h:173

To discover the proper macro check for section RETURN VALUE in the function man page.
Consider the definition of the following bash function to make it easier to fetch this information.

function manrv() {
man -P 'less -p ^"RETURN VALUE"' $*
}

Handle Defensive Programming of C/C++ library functions include file.

Remarks
Handles defensive programming of native libraries

group of hdp

Author
Miguel Oliveira e Silva
Date
2026
Version
1.0

Macro Definition Documentation

◆ MO

#define MO (   func_call)

Macro Minus One: applicable to functions returning -1 for error indication (cause stored in errno).

Parameters
[in]func_call[int] function call (or expression with the result of a library function call)

Same as errMO.

◆ Ne

#define Ne (   func_call)

Macro Negative: applicable to functions returning a negative integer value for error indication (cause stored in errno).

Parameters
[in]func_call[int] function call (or expression with the result of a library function call)

Same as errNe.

◆ Nu

#define Nu (   func_call)

Macro Null: applicable to functions returning NULL for error indication (cause stored in errno).

Parameters
[in]func_call[void*] function call (or expression with the result of a library function call)

Same as errNu.

◆ NZ

#define NZ (   func_call)

Macro Not Zero: applicable to functions returning a value different to 0 for error indication (error in function result using errno code values).

Parameters
[in]func_call[int] function call (or expression with the result of a library function call)

Same as errNZ.

◆ EF

#define EF (   func_call)

Macro End of File: applicable to functions returning EOF for error indication (cause stored in errno).

Parameters
[in]func_call[int] function call (or expression with the result of a library function call)

Same as errEF.

◆ MOP

#define MOP (   func_call)

Macro Minus One Pointer: applicable to functions returning (void*)-1 for error indication (cause stored in errno).

Parameters
[in]func_call[void*] function call (or expression with the result of a library function call)

Same as errMOP.

◆ errMO

#define errMO   MO

Same as MO

◆ errNe

#define errNe   Ne

Same as Ne

◆ errNu

#define errNu   Nu

Same as Nu

◆ errNZ

#define errNZ   NZ

Same as NZ

◆ errEF

#define errEF   EF

Same as EF

◆ errMOP

#define errMOP   MOP

Same as MOP