Commit 084afa90 authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

util: copy errno before calling stdlib's functions



Functions are allowed to change the value of errno, even when they're
successful.  So, save it before calling other functions to ensure it's
not overwritten.

Additionally, scan-build complains that these errnos can still be zero,
even though the manual says that they're always set if an error is
encountered.  To keep it quiet, added a couple of assertions that check
it.

Fixes: #3453

Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I76209bc1970f42cc8d52446fa651d5a9fee3d70f
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24298


Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Bypass-Merge-Requirements: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
parent 2cf63d58
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ read_sysfs_attribute(char **attribute_p, const char *format, va_list args)
	char *path;
	size_t len = 0;
	ssize_t read;
	int errsv;

	path = spdk_vsprintf_alloc(format, args);
	if (path == NULL) {
@@ -74,19 +75,23 @@ read_sysfs_attribute(char **attribute_p, const char *format, va_list args)
	}

	file = fopen(path, "r");
	errsv = errno;
	free(path);
	if (file == NULL) {
		return -errno;
		assert(errsv != 0);
		return -errsv;
	}

	*attribute_p = NULL;
	read = getline(attribute_p, &len, file);
	errsv = errno;
	fclose(file);
	attribute = *attribute_p;
	if (read == -1) {
		/* getline man page says line should be freed even on failure. */
		free(attribute);
		return -errno;
		assert(errsv != 0);
		return -errsv;
	}

	/* len is the length of the allocated buffer, which may be more than