igt.cocci: check the return values of various functions

Add rules to fix unused-result warnings when compiling with
_FORTIFY_SOURCE defined and apply them to the library and tests.

Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Thomas Wood <thomas.wood@intel.com>
This commit is contained in:
Thomas Wood 2015-03-25 16:42:57 +00:00
parent 0c4dd28d2d
commit 47f6b1305c
11 changed files with 58 additions and 29 deletions

View File

@ -173,3 +173,31 @@ int E3, E4;
- igt_assert(E3 < E4); - igt_assert(E3 < E4);
+ igt_assert_lt(E3, E4); + igt_assert_lt(E3, E4);
) )
// avoid unused-result warnings when compiling with _FORTIFY_SOURCE defined
@@
identifier func =~ "^(read|write)$";
expression list[2] E;
expression size;
@@
-func(E, size);
+igt_assert_eq(func(E, size), size);
@@
expression ptr, size, nmemb, stream;
@@
-fread(ptr, size, nmemb, stream);
+igt_assert_eq(fread(ptr, size, nmemb, stream), nmemb);
@@
expression list E;
@@
-fgets(E);
+igt_assert_neq(fgets(E), NULL);
@@
identifier func =~ "^v?asprintf$";
expression list E;
@@
-func(E);
+igt_assert_neq(func(E), -1);

View File

@ -417,13 +417,15 @@ static void low_mem_killer_disable(bool disable)
/* writing 9999 to this module parameter effectively diables the /* writing 9999 to this module parameter effectively diables the
* low memory killer. This is not a real file, so we dont need to * low memory killer. This is not a real file, so we dont need to
* seek to the start or truncate it */ * seek to the start or truncate it */
write(fd, no_lowmem_killer, sizeof(no_lowmem_killer)); igt_assert_eq(write(fd, no_lowmem_killer, sizeof(no_lowmem_killer)),
sizeof(no_lowmem_killer));
close(fd); close(fd);
} else { } else {
/* just re-enstate the original settings */ /* just re-enstate the original settings */
fd = open(adj_fname, O_WRONLY); fd = open(adj_fname, O_WRONLY);
igt_assert(fd != -1); igt_assert(fd != -1);
write(fd, prev_adj_scores, adj_scores_len); igt_assert_eq(write(fd, prev_adj_scores, adj_scores_len),
adj_scores_len);
close(fd); close(fd);
} }
@ -864,7 +866,8 @@ void __igt_skip_check(const char *file, const int line,
char *err_str = NULL; char *err_str = NULL;
if (err) if (err)
asprintf(&err_str, "Last errno: %i, %s\n", err, strerror(err)); igt_assert_neq(asprintf(&err_str, "Last errno: %i, %s\n", err, strerror(err)),
-1);
if (f) { if (f) {
static char *buf; static char *buf;
@ -874,7 +877,7 @@ void __igt_skip_check(const char *file, const int line,
free(buf); free(buf);
va_start(args, f); va_start(args, f);
vasprintf(&buf, f, args); igt_assert_neq(vasprintf(&buf, f, args), -1);
va_end(args); va_end(args);
igt_skip("Test requirement not met in function %s, file %s:%i:\n" igt_skip("Test requirement not met in function %s, file %s:%i:\n"
@ -1407,10 +1410,11 @@ static void fatal_sig_handler(int sig)
continue; continue;
if (handled_signals[i].name_len) { if (handled_signals[i].name_len) {
write(STDERR_FILENO, "Received signal ", 16); igt_assert_eq(write(STDERR_FILENO, "Received signal ", 16),
write(STDERR_FILENO, handled_signals[i].name, 16);
handled_signals[i].name_len); igt_assert_eq(write(STDERR_FILENO, handled_signals[i].name, handled_signals[i].name_len),
write(STDERR_FILENO, ".\n", 2); handled_signals[i].name_len);
igt_assert_eq(write(STDERR_FILENO, ".\n", 2), 2);
} }
break; break;

View File

@ -273,7 +273,7 @@ static bool igt_pipe_crc_do_start(igt_pipe_crc_t *pipe_crc)
sprintf(buf, "pipe %s %s", kmstest_pipe_name(pipe_crc->pipe), sprintf(buf, "pipe %s %s", kmstest_pipe_name(pipe_crc->pipe),
pipe_crc_source_name(pipe_crc->source)); pipe_crc_source_name(pipe_crc->source));
errno = 0; errno = 0;
write(pipe_crc->ctl_fd, buf, strlen(buf)); igt_assert_eq(write(pipe_crc->ctl_fd, buf, strlen(buf)), strlen(buf));
if (errno != 0) if (errno != 0)
return false; return false;
@ -285,7 +285,7 @@ static void igt_pipe_crc_pipe_off(int fd, enum pipe pipe)
char buf[32]; char buf[32];
sprintf(buf, "pipe %s none", kmstest_pipe_name(pipe)); sprintf(buf, "pipe %s none", kmstest_pipe_name(pipe));
write(fd, buf, strlen(buf)); igt_assert_eq(write(fd, buf, strlen(buf)), strlen(buf));
} }
static void igt_pipe_crc_reset(void) static void igt_pipe_crc_reset(void)
@ -417,7 +417,7 @@ void igt_pipe_crc_stop(igt_pipe_crc_t *pipe_crc)
char buf[32]; char buf[32];
sprintf(buf, "pipe %s none", kmstest_pipe_name(pipe_crc->pipe)); sprintf(buf, "pipe %s none", kmstest_pipe_name(pipe_crc->pipe));
write(pipe_crc->ctl_fd, buf, strlen(buf)); igt_assert_eq(write(pipe_crc->ctl_fd, buf, strlen(buf)), strlen(buf));
} }
static bool pipe_crc_init_from_string(igt_crc_t *crc, const char *line) static bool pipe_crc_init_from_string(igt_crc_t *crc, const char *line)

View File

@ -434,9 +434,8 @@ bool kmstest_force_connector(int drm_fd, drmModeConnector *connector,
break; break;
} }
asprintf(&path, "%s-%d/force", igt_assert_neq(asprintf(&path, "%s-%d/force", kmstest_connector_type_str(connector->connector_type), connector->connector_type_id),
kmstest_connector_type_str(connector->connector_type), -1);
connector->connector_type_id);
debugfs_fd = igt_debugfs_open(path, O_WRONLY | O_TRUNC); debugfs_fd = igt_debugfs_open(path, O_WRONLY | O_TRUNC);
if (debugfs_fd == -1) { if (debugfs_fd == -1) {
@ -494,9 +493,8 @@ void kmstest_force_edid(int drm_fd, drmModeConnector *connector,
char *path; char *path;
int debugfs_fd, ret; int debugfs_fd, ret;
asprintf(&path, "%s-%d/edid_override", igt_assert_neq(asprintf(&path, "%s-%d/edid_override", kmstest_connector_type_str(connector->connector_type), connector->connector_type_id),
kmstest_connector_type_str(connector->connector_type), -1);
connector->connector_type_id);
debugfs_fd = igt_debugfs_open(path, O_WRONLY | O_TRUNC); debugfs_fd = igt_debugfs_open(path, O_WRONLY | O_TRUNC);
free(path); free(path);
@ -910,9 +908,8 @@ static void igt_output_refresh(igt_output_t *output)
if (!output->name) { if (!output->name) {
drmModeConnector *c = output->config.connector; drmModeConnector *c = output->config.connector;
asprintf(&output->name, "%s-%d", igt_assert_neq(asprintf(&output->name, "%s-%d", kmstest_connector_type_str(c->connector_type), c->connector_type_id),
kmstest_connector_type_str(c->connector_type), -1);
c->connector_type_id);
} }
LOG(display, "%s: Selecting pipe %s\n", output->name, LOG(display, "%s: Selecting pipe %s\n", output->name,

View File

@ -255,7 +255,7 @@ intel_purge_vm_caches(void)
if (fd < 0) if (fd < 0)
return; return;
write(fd, "3\n", 2); igt_assert_eq(write(fd, "3\n", 2), 2);
close(fd); close(fd);
} }

View File

@ -30,7 +30,7 @@
static void dump_batch(struct intel_batchbuffer *batch) { static void dump_batch(struct intel_batchbuffer *batch) {
int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT, 0666); int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT, 0666);
if (fd != -1) { if (fd != -1) {
write(fd, batch->buffer, 4096); igt_assert_eq(write(fd, batch->buffer, 4096), 4096);
fd = close(fd); fd = close(fd);
} }
} }

View File

@ -31,7 +31,7 @@
static void dump_batch(struct intel_batchbuffer *batch) { static void dump_batch(struct intel_batchbuffer *batch) {
int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT, 0666); int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT, 0666);
if (fd != -1) { if (fd != -1) {
write(fd, batch->buffer, 4096); igt_assert_eq(write(fd, batch->buffer, 4096), 4096);
fd = close(fd); fd = close(fd);
} }
} }

View File

@ -198,7 +198,7 @@ static bool fbc_enabled(data_t *data)
status = igt_debugfs_fopen("i915_fbc_status", "r"); status = igt_debugfs_fopen("i915_fbc_status", "r");
igt_assert(status); igt_assert(status);
fread(str, sizeof(str) - 1, 1, status); igt_assert_eq(fread(str, sizeof(str) - 1, 1, status), 1);
fclose(status); fclose(status);
return strstr(str, "FBC enabled") != NULL; return strstr(str, "FBC enabled") != NULL;
} }
@ -510,7 +510,7 @@ igt_main
status = igt_debugfs_fopen("i915_fbc_status", "r"); status = igt_debugfs_fopen("i915_fbc_status", "r");
igt_require_f(status, "No i915_fbc_status found\n"); igt_require_f(status, "No i915_fbc_status found\n");
fread(buf, sizeof(buf), 1, status); igt_assert_eq(fread(buf, sizeof(buf), 1, status), sizeof(buf));
fclose(status); fclose(status);
buf[sizeof(buf) - 1] = '\0'; buf[sizeof(buf) - 1] = '\0';
igt_require_f(!strstr(buf, "unsupported by this chipset") && igt_require_f(!strstr(buf, "unsupported by this chipset") &&

View File

@ -101,7 +101,7 @@ static void query(int fd, bool busy)
busy ? "busy" : "idle", elapsed(&start, &end, count)); busy ? "busy" : "idle", elapsed(&start, &end, count));
if (busy) if (busy)
read(fd, buf, sizeof(buf)); igt_assert_eq(read(fd, buf, sizeof(buf)), sizeof(buf));
} }
igt_main igt_main

View File

@ -45,12 +45,12 @@ static void disable_audio_runtime_pm(void)
fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_WRONLY); fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_WRONLY);
if (fd >= 0) { if (fd >= 0) {
write(fd, "1\n", 2); igt_assert_eq(write(fd, "1\n", 2), 2);
close(fd); close(fd);
} }
fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_WRONLY); fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_WRONLY);
if (fd >= 0) { if (fd >= 0) {
write(fd, "auto\n", 5); igt_assert_eq(write(fd, "auto\n", 5), 5);
close(fd); close(fd);
} }
/* Give some time for it to react. */ /* Give some time for it to react. */

View File

@ -61,7 +61,7 @@ static int find_and_open_devices(void)
if (!fl) if (!fl)
break; break;
fgets(vendor_id, 8, fl); igt_assert_neq(fgets(vendor_id, 8, fl), NULL);
fclose(fl); fclose(fl);
venid = strtoul(vendor_id, NULL, 16); venid = strtoul(vendor_id, NULL, 16);