21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

that uses an unsigned 32-bit value will wrap around every 49.71 days or so), the<br />

result of your test is likely to be wrong!<br />

In any case, you should be keeping track of wrap-arounds and taking appropriate<br />

measures when they occur. Often, when you’re using a real-time clock, you can simply<br />

use a clock with more precision. For example, recent x86 chips offer the RDTSC<br />

instruction, which provides 64 bits of precision. (See Recipe 4.14.)<br />

See Also<br />

Recipe 4.14<br />

3.6 Using Environment Variables Securely<br />

<strong>Problem</strong><br />

You need to obtain the value of, alter the value of, or delete an environment variable.<br />

Solution<br />

A process inherits its environment variables from its parent process. While the parent<br />

process most often will not do anything to tarnish the environment passed on to<br />

its children, your program’s environment variables are still external inputs, and you<br />

must therefore treat them as such.<br />

The process that parents your own process could be a malicious process that has<br />

manipulated the environment in an attempt to confuse your program and exploit<br />

that confusion to nefarious ends. As much as possible, it is best to avoid depending<br />

on the environment, but we recognize that is not always possible.<br />

Discussion<br />

In the following subsections, we’ll look at obtaining the value of an environment<br />

variable as well as changing and deleting environment variables.<br />

Obtaining the value of an environment variable<br />

The normal means by which you obtain the value of an environment variable is by<br />

calling getenv( ) with the name of the environment variable whose value is to be<br />

retrieved. The problem with getenv( ) is that it simply returns a pointer into the environment,<br />

rather than returning a copy of the environment variable’s value.<br />

If you do not immediately make a copy of the value returned by getenv( ), but<br />

instead store the pointer somewhere for later use, you could end up with a dangling<br />

92 | Chapter 3: Input Validation<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!