Content
    Pre- and postfixes for printf and scanf

Harmful C Functions and their replacements

Pre- and postfixes for printf and scanf

There are quite a lot of functions in the printf and scanff amily. It might not be very clear when to use which of them.

Here is an explanation of the functions in the Standard C Library.

Prefixes

Most prefixed define where the function reads (scanf) or wirtes (printf) data from or to:

  • no prefix: use STDIN or STDOUT
  • f: use a FILE stream
  • s: use a char buffer (string)
  • sn: same as s but checks for buffer size (only printf)
  • v: takes a va_list instead ... (ellipsis), can be combined with the other prefixes

Postfix

The C11 standard introduced addtional functions with the _s postfix which do some checks on the data.

Overview

Output

So we get following printf-like functions:

Outputellipsisva_list
STDOUTprintf(_s)vprintf(_s)
filefprintf(_s)vfprintf(_s)
char buffersprintf(_s)vsprintf(_s)
char buffer with sizesnprintf(_s)vsnprintf(_s)

The functions with _s postfix should be preferred if available (C11).

The sprintf and vsprintf functions should not be used since the can result in stack overflow. They are also suffer from string vulnerability.

Use snpritf or vsnprintf instead. If possible with _s postfix.

And force null-termination manually 1.

Input

And we get following scanf-like functions:

Inputellipsisva_list
STDINscanf(_s)vscanf(_s)
filefscanf(_s)vfscanf(_s)
char buffersscanf(_s)vsscanf(_s)

The functions with _s postfix should be preferred if available (C11).

And force null-termination manually 1.

OpenBSD

The OpenBSD kernel library defines some additional functions that are safer than their counterparts in the standard library.

C standard libraryOpenBSD kernellibrary
Copying stringstrcpystrlcpy
Applying (concatenating) stringstrcatstrlcat

Replacements

These C functions suffer buffer overflow problems:

OriginalReplacement
gets()fgets()
cuserid()getlogin() or getpwuid()
scanf() familySee 2 and 3, use functions with _spostfix (C11)
sprintf()snprintf(), use functions with _spostfix (C11)
vsprintf()vsnprintf(), use functions with _spostfix (C11)
strcat()strncat()
strcpy()strncpy()
streadd(), strtrns(), strecpy()Check lengths of buffers or use standard library functions
getwd()getcwd()

See also 4

References



  • Category

  • Programming

  • Tags

  • C
    C++

  • Created

  • 18. April 2015


  • Modified

  • 16. May 2022