Browse Source

Fix issues found by Coverity, closes #206.

Ahsan Fayaz 4 years ago
parent
commit
510a0a6ae2
3 changed files with 37 additions and 21 deletions
  1. 22 15
      common/common-governors.c
  2. 2 1
      daemon/gamemode-wine.c
  3. 13 5
      daemon/gamemoded.c

+ 22 - 15
common/common-governors.c

@@ -122,22 +122,29 @@ const char *get_gov_state(void)
 		long length = ftell(f);
 		fseek(f, 0, SEEK_SET);
 
-		char contents[length];
-
-		if (fread(contents, 1, (size_t)length, f) > 0) {
-			/* Files have a newline */
-			strtok(contents, "\n");
-			if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
-				/* Don't handle the mixed case, this shouldn't ever happen
-				 * But it is a clear sign we shouldn't carry on */
-				LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
-				fclose(f);
-				return "malformed";
+		if (length == -1)
+		{
+			LOG_ERROR("Failed to seek file %s\n", gov);
+		}
+		else
+		{
+			char contents[length];
+
+			if (fread(contents, 1, (size_t)length, f) > 0) {
+				/* Files have a newline */
+				strtok(contents, "\n");
+				if (strlen(governor) > 0 && strncmp(governor, contents, 64) != 0) {
+					/* Don't handle the mixed case, this shouldn't ever happen
+					* But it is a clear sign we shouldn't carry on */
+					LOG_ERROR("Governors malformed: got \"%s\", expected \"%s\"", contents, governor);
+					fclose(f);
+					return "malformed";
+				}
+
+				strncpy(governor, contents, sizeof(governor) - 1);
+			} else {
+				LOG_ERROR("Failed to read contents of %s\n", gov);
 			}
-
-			strncpy(governor, contents, sizeof(governor) - 1);
-		} else {
-			LOG_ERROR("Failed to read contents of %s\n", gov);
 		}
 
 		fclose(f);

+ 2 - 1
daemon/gamemode-wine.c

@@ -224,7 +224,8 @@ char *game_mode_resolve_wine_preloader(const char *exe, const pid_t pid)
 		goto fail;
 
 error_cleanup:
-	game_mode_close_proc(proc_fd);
+	if (proc_fd != INVALID_PROCFD)
+		game_mode_close_proc(proc_fd);
 	free(wineprefix);
 	return wine_exe;
 

+ 13 - 5
daemon/gamemoded.c

@@ -132,11 +132,19 @@ static void daemonize(const char *name)
 	/* replace standard file descriptors by /dev/null */
 	int devnull_r = open("/dev/null", O_RDONLY);
 	int devnull_w = open("/dev/null", O_WRONLY);
-	dup2(devnull_r, STDIN_FILENO);
-	dup2(devnull_w, STDOUT_FILENO);
-	dup2(devnull_w, STDERR_FILENO);
-	close(devnull_r);
-	close(devnull_w);
+
+	if (devnull_r == -1 || devnull_w == -1)
+	{
+		LOG_ERROR("Failed to redirect standard input and output to /dev/null\n");
+	}
+	else
+	{
+		dup2(devnull_r, STDIN_FILENO);
+		dup2(devnull_w, STDOUT_FILENO);
+		dup2(devnull_w, STDERR_FILENO);
+		close(devnull_r);
+		close(devnull_w);
+	}
 }
 
 /**