TSDuck v3.44-4659
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches

Canary class to detect memory corruption in instable environments. More...

#include <tsCanary.h>

Public Member Functions

 Canary ()=default
 Constructor with a "good" default value.
 
 ~Canary ()
 The destructor sets a "bad" canary to detect use-after-free in case of incorrect order of usage.
 

Static Public Member Functions

static const UCharError (const Canary *c)
 Check that a Canary pointer is valid and get an error message.
 
static bool IsValid (const Canary *c)
 Check that a Canary pointer is valid.
 
static const UCharLogError (const Canary *c)
 Check that a Canary pointer is valid, get and log an error message on standard error.
 

Detailed Description

Canary class to detect memory corruption in instable environments.

Some libraries or operating systems associate a "user-pointer" to an event or some equivalent concept. This user-pointer usually is a user-allocated data structure. It is passed back to the application by the library or operating system at some point in the future.

The application must ensure that the user-allocated structure remains valid as long as the user-pointer is stored in the library or operating system. Otherwise, the application may receive the address of a structure which is no longer valid.

In some cases, it is complicated to understand how long this user pointer is stored. To track potential errors in our interpretation of the various libraries or operating systems logics, we use some internal consistency checks using canaries, values which are altered when the structure is no longer valid.

User-defined data structures which are used as user-pointer for external libraries or operating systems should use a Canary field as first field.

Member Function Documentation

◆ Error()

static const UChar * ts::Canary::Error ( const Canary c)
static

Check that a Canary pointer is valid and get an error message.

Parameters
[in]cThe address of something that is expected to be a valid Canary.
Returns
A null pointer if c seems a valid pointer, the address of a static error message otherwise.

◆ LogError()

static const UChar * ts::Canary::LogError ( const Canary c)
static

Check that a Canary pointer is valid, get and log an error message on standard error.

Parameters
[in]cThe address of something that is expected to be a valid Canary.
Returns
A null pointer if c seems a valid pointer, the address of a static error message otherwise. In case of invalid pointer, the error message is also displayed on standard error.

◆ IsValid()

static bool ts::Canary::IsValid ( const Canary c)
inlinestatic

Check that a Canary pointer is valid.

Parameters
[in]cThe address of something that is expected to be a valid Canary.
Returns
True if c seems a valid pointer, false otherwise.

The documentation for this class was generated from the following file: