Commit 147abe51 authored by Aleksey Marchuk's avatar Aleksey Marchuk Committed by Tomasz Zawadzki
Browse files

lib/event: Check possible error returned by fscanf



Scanbuild:
app.c:207:8: warning: File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior [unix.Stream]
  207 |                 rc = fscanf(f, "cpu%li %li %*i %li %*i %*i %li %li %*i %*i %*i\n",
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208 |                             &cpu, user, sys, irq, &soft_irq);
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
app.c:207:8: warning: Read function called when stream is in EOF state. Function has no effect [unix.Stream]
  207 |                 rc = fscanf(f, "cpu%li %li %*i %li %*i %*i %li %li %*i %*i %*i\n",
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208 |                             &cpu, user, sys, irq, &soft_irq);
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
app.c:220:7: warning: The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage [core.uninitialized.Assign]
  220 |         *irq += soft_irq;
      |         ~~~~ ^

Change-Id: I545675093bcfac241114c1b55f8835fcda3ffd99
Signed-off-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-on: https://review.spdk.io/c/spdk/spdk/+/26865


Tested-by: default avatarSPDK Automated Test System <spdkbot@gmail.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarJacek Kalwas <jacek.kalwas@nutanix.com>
parent d76dba8c
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -193,7 +193,7 @@ static int
parse_proc_stat(unsigned int core, uint64_t *user, uint64_t *sys, uint64_t *irq)
{
	FILE *f;
	uint64_t i, soft_irq, cpu = 0;
	uint64_t i, soft_irq = 0, cpu = 0;
	int rc, found = 0;

	f = fopen("/proc/stat", "r");
@@ -206,6 +206,10 @@ parse_proc_stat(unsigned int core, uint64_t *user, uint64_t *sys, uint64_t *irq)
		 * cpu;user;nice;system;idle;iowait;irq;softirq;steal;guest;guest_nice */
		rc = fscanf(f, "cpu%li %li %*i %li %*i %*i %li %li %*i %*i %*i\n",
			    &cpu, user, sys, irq, &soft_irq);
		if (rc == EOF) {
			fclose(f);
			return -1;
		}
		if (rc != 5) {
			continue;
		}