Commit a93a149c authored by Jim Harris's avatar Jim Harris Committed by Konrad Sztyber
Browse files

log: use va_copy to avoid using undefined va_list



According to vnsprintf man page, `ap` parameter is undefined after
this call returns. This means it is invalid to then pass it to
vasprintf() if vnsprintf() returns something larger than MAX_TMPBUF.

Newer ASAN catches this problem immediately. Adding the va_copy()
fixes the problem.

Fixes issue #3507.

Signed-off-by: default avatarJim Harris <jim.harris@samsung.com>
Change-Id: I930b7eb2bd454c3834d988162bf0ef886e2b47de
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24673


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Community-CI: Mellanox Build Bot
parent bc1f619b
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ spdk_vlog(enum spdk_log_level level, const char *file, const int line, const cha
	int severity = LOG_INFO;
	char *buf, _buf[MAX_TMPBUF], *ext_buf = NULL;
	char timestamp[64];
	va_list ap_copy;
	int rc;

	if (g_log) {
@@ -156,12 +157,13 @@ spdk_vlog(enum spdk_log_level level, const char *file, const int line, const cha

	buf = _buf;

	va_copy(ap_copy, ap);
	rc = vsnprintf(_buf, sizeof(_buf), format, ap);
	if (rc > MAX_TMPBUF) {
		/* The output including the terminating was more than MAX_TMPBUF bytes.
		 * Try allocating memory large enough to hold the output.
		 */
		rc = vasprintf(&ext_buf, format, ap);
		rc = vasprintf(&ext_buf, format, ap_copy);
		if (rc < 0) {
			/* Failed to allocate memory. Allow output to be truncated. */
		} else {