29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Compiler</strong> Coding Practices<br />

5.80 snprintf family of functions in C99<br />

<strong>Using</strong> <strong>the</strong> sprintf family of functions found in <strong>the</strong> C90 standard header can be<br />

dangerous. In <strong>the</strong> statement:<br />

sprintf(buffer, size, "Error %d: Cannot open file '%s'", errno, filename);<br />

<strong>the</strong> variable size specifies <strong>the</strong> minimum number of characters to be inserted into buffer.<br />

Consequently, more characters can be output than might fit in <strong>the</strong> memory allocated to <strong>the</strong><br />

string.<br />

The snprintf functions found in <strong>the</strong> C99 version of are safe versions of <strong>the</strong> sprintf<br />

functions that prevent buffer overrun. In <strong>the</strong> statement:<br />

snprintf(buffer, size, "Error %d: Cannot open file '%s'", errno, filename);<br />

<strong>the</strong> variable size specifies <strong>the</strong> maximum number of characters that can be inserted into buffer.<br />

The buffer can never be overrun, provided its size is always greater than <strong>the</strong> size specified by<br />

size.<br />

5.80.1 See also<br />

Concepts<br />

• New library features of C99 on page 5-79.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-98<br />

ID061811<br />

Non-Confidential

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

Saved successfully!

Ooh no, something went wrong!