Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example assigns the address of the printf() function to the log_fn function pointer, which can be allocated in the stack or data segment.

Code Block
bgColor#FFCCCC
langc
int (*log_fn)(const char *, ...) = printf;
/* ... */
log_fn("foo");

...

Microsoft Windows provides the EncodePointer() and DecodePointer() function that encrypt and decrypt pointers using a secret that is unique to the given process.

Code Block
bgColor#ccccff
langc
int (*log_fn)(const char *, ...) = EncodePointer(printf);
/* ... */
DecodePointer(log_fn)("foo");

...

The encode_pointer function shall perform a transformation on the pf argument, such that the decode_pointer function shall reverse that transformation. Thus, for any pointer to function pfun,

Code Block
decode_pointer(encode_pointer( (void(*)()) pfun )

when converted to the type of pfun, shall equal pfun.

Code Block
bgColor#ccccff
langc
int (*log_fn)(const char *, ...) = encode_pointer(printf);
/* ... */
decode_pointer(log_fn)("foo");

...