Skip to content

Split *s*printf(3) from printf(3), and document the snprintf(,-1,) is UB

Alejandro Colomar linux-man 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 4 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2man/man3/*printf.3: Split *s*printf(3) from printf(3)man/man3/printf.3 man/man3/snprintf.3 man/man3/sprintf.3 +2 more+327 -2200 replies
2/2man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is essentially UBman/man3/snprintf.3+20 -14 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] Split *s*printf(3) from printf(3), and document the snprintf(,-1,) is UB

Hi!

Serge and I were reviewing some code, and we realized that it seems quite easy to induce a buffer overflow using snprintf(3).

We solved our problems by checking that the size is non-negative in a wrapper inline function.

	// vstprintf - va_list string truncate print formatted
	inline int
	vstprintf(char *restrict s, ssize_t size, const char *restrict fmt, va_list ap)
	{
		int  len;

		if (size <= 0 || size > INT_MAX) {
			errno = EOVERFLOW;
			return -1;
		}
		len = vsnprintf(s, size, fmt, ap);
		if (len == -1) {
			if (errno == EOVERFLOW)
				errno = E2BIG;
			return -1;
		}
		if (len >= size) {
			errno = E2BIG;
			return -1;
		}

		return len;
	}

[v]snprintf(3) seems to be even worse than I thought it was. Let's document this.

Alejandro Colomar (2):
man/man3/*printf.3: Split *s*printf(3) from printf(3) man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is
essentially UB

 man/man3/printf.3    | 223 +---------------------------
 man/man3/snprintf.3  | 337 ++++++++++++++++++++++++++++++++++++++++++-
 man/man3/sprintf.3   |   2 +-
 man/man3/vsnprintf.3 |   2 +-
 man/man3/vsprintf.3  |   2 +-
 5 files changed, 346 insertions(+), 220 deletions(-)

Range-diff:

-:  --------- > 1:  a654dcfc2 man/man3/*printf.3: Split *s*printf(3) from printf(3)
-:  --------- > 2:  d00ed9434 man/man3/snprintf.3: BUGS: snprintf(3) with a negative size is essentially UB

base-commit: 2a5edb7e59ae024d1969e7379c078a58b5f50b80

Signature · 1 line
2.51.0