• Please review our updated Terms and Rules here

How did C programmers use sprintf without overflow/error before snprintf existed?

charnitz

Experienced Member
Joined
Nov 17, 2020
Messages
55
Location
North America
Did programmers just check the format specifications in the format string and/or test with max values to determine how many characters sprintf would output to the string?
 
We were taught to check the values beforehand. You always should verify parameters.
 
You have to understand the original reasoning behind C's I/O library. It was initially a "quick and dirty" approach to providing some utility and file I/O. Recall that early Algol had no defined I/O statements! Originally, the thinking was that I/O is implementation-dependent and could be accommodated through user-provided procedure calls. The same goes for string (well, array of characters) handling (e.g. strcat). So you use the length-specified stuff when available and appropriate; otherwise, you exercise caution when using unspecified length stuff.

There are lots of ways C programs can fall into a pit. Consider, for example, the simple statement:

Code:
  static char *x = "Hello";
  strcpy(x+1, x);

Recall that C was originally an alternative to assembly.
 
Back
Top