Guard Byte - Principle

Principle

When a program is compiled for debugging, all memory allocations are prefixed and postfixed by guard bytes. Special memory allocation routines may then perform additional tasks to determine unwanted read and write attempts outside the allocated memory. These extra bytes help to detect that the program is writing into (or even reading from) inappropriate memory areas, potentially causing buffer overflows. In case of accessing these bytes by the program's algorithm, the programmer is warned with information assisting him/her to locate the problem.

Checking for the inappropriate access to the guard bytes may be done in two ways:

  • by setting a memory breakpoint on a condition of write and/or read to those bytes, or
  • by pre-initializing the guard bytes with specific values and checking the values upon deallocation.

The first way is possible only with a debugger that handles such breakpoints, but significantly increases the chance of locating the problem. The second way does not require any debuggers or special environments and can be done even on other computers, but the programmer is alerted about the overflow only upon the deallocation, which is sometimes quite late.

Because guard bytes require additional code to be executed and additional memory to be allocated, they are used only when the program is compiled for debugging. When compiled as a release, guard bytes are not used at all, neither the routines working with them.

Read more about this topic:  Guard Byte

Famous quotes containing the word principle:

    For me chemistry represented an indefinite cloud of future potentialities which enveloped my life to come in black volutes torn by fiery flashes, like those which had hidden Mount Sinai. Like Moses, from that cloud I expected my law, the principle of order in me, around me, and in the world.... I would watch the buds swell in spring, the mica glint in the granite, my own hands, and I would say to myself: “I will understand this, too, I will understand everything.”
    Primo Levi (1919–1987)

    We seem to be pariahs alike in the visible and the invisible world, with no foothold anywhere, though by every principle of government and religion we should have an equal place on this planet.
    Elizabeth Cady Stanton (1815–1902)

    On principle I dislike an oath which requires a man to swear he has not done wrong. It rejects the Christian principle of forgiveness on terms of repentance. I think it is enough if the man does no wrong hereafter.
    Abraham Lincoln (1809–1865)