Skip to contentSkip to conversation

Suppress compiler warnings on write()

PATCH Applied Namhyung Kim 5 participants 8 messages Dec 3, 2012 – Jan 31, 2013

lore
Share & tools
lore

Namhyung Kim

author
#0 lore raw

PATCH 1/3 Suppress compiler warnings on write()

Modern linux distribution forces to check result of write() call. It emits following warnings:

CXX calibrate/powertop-calibrate.o
calibrate/calibrate.cpp: In function ‘void* burn_disk(void*)’:
calibrate/calibrate.cpp:297: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
...
CXX measurement/powertop-extech.o
measurement/extech.cpp: In member function ‘void extech_power_meter::measure()’:
measurement/extech.cpp:290: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result

Signed-off-by Namhyung Kim <namhyung(a)gmail.com>
---
 src/calibrate/calibrate.cpp |    3 ++-
 src/measurement/extech.cpp  |    3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
src/calibrate/calibrate.cpp +2 -1
index 5457cf8..2d4c1af 100644
@@ -294,7 +294,8 @@ static void *burn_disk(void *dummy)
 
 	while (!stop_measurement) {
 		lseek(fd, 0, SEEK_SET);
-		write(fd, buffer, 64*1024);
+		if (write(fd, buffer, 64*1024))
+			/* make gcc silent */;
 		fdatasync(fd);
 	}
 	close(fd);
src/measurement/extech.cpp +2 -1
index 4e8bf8a..b9e95ca 100644
@@ -287,7 +287,8 @@ extech_power_meter::extech_power_meter(const char *extech_name)
 void extech_power_meter::measure(void)
 {
 	/* trigger the extech to send data */
-	write(fd, " ", 1);
+	if (write(fd, " ", 1) < 0)
+		return;
 
 	rate = extech_read(fd);
 
Signature · 1 line
1.7.9.2
author
#1 lore raw

PATCH 2/3 Suppress compiler warnings on system()

As system(3) requires to check its return value, make a wrapper do_system() to suppress following warnings:

CXX tuning/powertop-bluetooth.o
tuning/bluetooth.cpp: In member function ‘virtual void bt_tunable::toggle()’:
tuning/bluetooth.cpp:179: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result tuning/bluetooth.cpp:182: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
CXX tuning/powertop-cpufreq.o
tuning/cpufreq.cpp: In member function ‘virtual void cpufreq_tunable::toggle()’:
tuning/cpufreq.cpp:118: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
...
CXX calibrate/powertop-calibrate.o
calibrate/calibrate.cpp: In function ‘void backlight_calibration()’:
calibrate/calibrate.cpp:408: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result calibrate/calibrate.cpp:410: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result calibrate/calibrate.cpp: In function ‘void idle_calibration()’:
calibrate/calibrate.cpp:416: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result calibrate/calibrate.cpp:418: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

Signed-off-by Namhyung Kim <namhyung(a)gmail.com>
---
 src/calibrate/calibrate.cpp |    8 ++++----
 src/lib.cpp                 |    5 +++++
 src/lib.h                   |    1 +
 src/tuning/bluetooth.cpp    |    4 ++--
 src/tuning/cpufreq.cpp      |    2 +-
 5 files changed, 13 insertions(+), 7 deletions(-)
src/calibrate/calibrate.cpp +4 -4
index 2d4c1af..c886b73 100644
@@ -406,17 +406,17 @@ static void backlight_calibration(void)
 		sleep(1);
 	}
 	printf(_("Calibrating idle\n"));
-	system("DISPLAY=:0 /usr/bin/xset dpms force off");
+	do_system("DISPLAY=:0 /usr/bin/xset dpms force off");
 	one_measurement(15, NULL);
-	system("DISPLAY=:0 /usr/bin/xset dpms force on");
+	do_system("DISPLAY=:0 /usr/bin/xset dpms force on");
 }
 
 static void idle_calibration(void)
 {
 	printf(_("Calibrating idle\n"));
-	system("DISPLAY=:0 /usr/bin/xset dpms force off");
+	do_system("DISPLAY=:0 /usr/bin/xset dpms force off");
 	one_measurement(15, NULL);
-	system("DISPLAY=:0 /usr/bin/xset dpms force on");
+	do_system("DISPLAY=:0 /usr/bin/xset dpms force on");
 }
 
 
src/lib.cpp +5 -0
index 9838c0b..ccba414 100644
@@ -441,3 +441,8 @@ int get_user_input(char *buf, unsigned sz)
 	/* to distinguish between getnstr error and empty line */
 	return ret || strlen(buf);
 }
+
+int do_system(const char *command)
+{
+	return system(command); /* make gcc slient */
+}
src/lib.h +1 -0
index 209421a..7002f14 100644
@@ -73,5 +73,6 @@ typedef void (*callback)(const char*);
 extern void process_directory(const char *d_name, callback fn);
 extern int utf_ok;
 extern int get_user_input(char *buf, unsigned sz);
+extern int do_system(const char *cmd);
 
 #endif
src/tuning/bluetooth.cpp +2 -2
index e0bdf12..b717610 100644
@@ -176,10 +176,10 @@ void bt_tunable::toggle(void)
 	good = good_bad();
 
 	if (good == TUNE_GOOD) {
-		system("/usr/sbin/hciconfig hci0 up &> /dev/null &");
+		do_system("/usr/sbin/hciconfig hci0 up &> /dev/null &");
 		return;
 	}
-	system("/usr/sbin/hciconfig hci0 down &> /dev/null");
+	do_system("/usr/sbin/hciconfig hci0 down &> /dev/null");
 }
 
 const char *bt_tunable::toggle_script(void)
src/tuning/cpufreq.cpp +1 -1
index df245ad..8a8d1c5 100644
@@ -115,7 +115,7 @@ void cpufreq_tunable::toggle(void)
 	int good;
 	good = good_bad();
 
-	system("/sbin/modprobe cpufreq_ondemand > /dev/null 2>&1");
+	do_system("/sbin/modprobe cpufreq_ondemand > /dev/null 2>&1");
 
 	if (good == TUNE_GOOD) {
 		dir = opendir("/sys/devices/system/cpu");
Signature · 1 line
1.7.9.2
author
#2 lore raw

PATCH 3/3 process: Get rid of unused dbg_printf_pevent_info()

In reply to Namhyung Kim

This function is not used, just remove it.
This fixes following warning:

CXX process/powertop-do_process.o
process/do_process.cpp:190: warning: ‘void dbg_printf_pevent_info(event_format*, pevent_record*)’ defined but not used

Signed-off-by Namhyung Kim <namhyung(a)gmail.com>
---
 src/process/do_process.cpp |   13 -------------
 1 file changed, 13 deletions(-)
src/process/do_process.cpp +0 -13
index 6ac0fae..a07b3f3 100644
@@ -187,19 +187,6 @@ int dont_blame_me(char *comm)
 	return 0;
 }
 
-static void dbg_printf_pevent_info(struct event_format *event, struct pevent_record *rec)
-{
-	static struct trace_seq s;
-
-	event->pevent->print_raw = 1;
-	trace_seq_init(&s);
-	pevent_event_info(&s, event, rec);
-	trace_seq_putc(&s, '\n');
-	trace_seq_terminate(&s);
-	fprintf(stderr, "%.*s", s.len, s.buffer);
-	trace_seq_destroy(&s);
-}
-
 static char * get_pevent_field_str(void *trace, struct event_format *event, struct format_field *field)
 {
 	unsigned long long offset, len;
Signature · 1 line
1.7.9.2
#3 lore raw

Re: [Powertop] [PATCH 3/3] process: Get rid of unused dbg_printf_pevent_info()

Quoted 25 lines · Namhyung Kim

This function is not used, just remove it.
This fixes following warning:

CXX process/powertop-do_process.o
process/do_process.cpp:190: warning: ‘void dbg_printf_pevent_info(event_format*, pevent_record*)’ defined but not used

Signed-off-by Namhyung Kim <namhyung(a)gmail.com>
---
 src/process/do_process.cpp |   13 -------------
 1 file changed, 13 deletions(-)
src/process/do_process.cpp +0 -6
index 6ac0fae..a07b3f3 100644
@@ -187,19 +187,6 @@ int dont_blame_me(char *comm)
 	return 0;
 }
 
-static void dbg_printf_pevent_info(struct event_format *event, struct pevent_record *rec)
-{
-	static struct trace_seq s;
-
-	event->pevent->print_raw = 1;
-	trace_seq_init(&s);

src/process/do_process.cpp int dont_blame_me(char *comm) · line 192

Quoted 10 lines
src/process/do_process.cpp +0 -7
-	pevent_event_info(&s, event, rec);
-	trace_seq_putc(&s, '\n');
-	trace_seq_terminate(&s);
-	fprintf(stderr, "%.*s", s.len, s.buffer);
-	trace_seq_destroy(&s);
-}
-
 static char * get_pevent_field_str(void *trace, struct event_format *event, struct format_field *field)
 {
 	unsigned long long offset, len;

this one looks good to me.

	-ss
Applied
#4 lore raw

Re: [Powertop] [PATCH 3/3] process: Get rid of unused dbg_printf_pevent_info()

In reply to Namhyung Kim
Quoted 27 lines · On Tue, 4 Dec 2012 00:13:20 +0900 Namhyung Kim <namhyung(a)gmail.com> wrote:

This function is not used, just remove it.
This fixes following warning:

CXX process/powertop-do_process.o
process/do_process.cpp:190: warning: ‘void dbg_printf_pevent_info(event_format*, pevent_record*)’ defined but not used

Signed-off-by Namhyung Kim <namhyung(a)gmail.com>
---
 src/process/do_process.cpp |   13 -------------
 1 file changed, 13 deletions(-)
src/process/do_process.cpp +0 -5
index 6ac0fae..a07b3f3 100644
@@ -187,19 +187,6 @@ int dont_blame_me(char *comm)
 	return 0;
 }
 
-static void dbg_printf_pevent_info(struct event_format *event,
 struct pevent_record *rec) -{
-	static struct trace_seq s;
-
-	event->pevent->print_raw = 1;
-	trace_seq_init(&s);

src/process/do_process.cpp int dont_blame_me(char *comm) · line 192

Quoted 10 lines
src/process/do_process.cpp +0 -7
-	pevent_event_info(&s, event, rec);
-	trace_seq_putc(&s, '\n');
-	trace_seq_terminate(&s);
-	fprintf(stderr, "%.*s", s.len, s.buffer);
-	trace_seq_destroy(&s);
-}
-
 static char * get_pevent_field_str(void *trace, struct event_format
 *event, struct format_field *field) {
 	unsigned long long offset, len;

Applied, thanks.

Kristen

#5 lore raw
In reply to Namhyung Kim

src/calibrate/calibrate.cpp static void *burn_disk(void *dummy) · line 299

Quoted 4 lines · Namhyung Kim
src/calibrate/calibrate.cpp +2 -1
-		write(fd, buffer, 64*1024);
+		if (write(fd, buffer, 64*1024))
+			/* make gcc silent */;
  		fdatasync(fd);

I'm sorry, but no.
This is waaaaaaaaaaay worse than leaving the compiler warning.
The compiler warning is real, and is a reminder that we need to improve the code "just make it shut up" patches are a really bad idea, and this is the perfect example of that.

author
#6 lore raw

Hi Arjan,

2012-12-03 (월), 08:14 -0800, Arjan van de Ven:

Quoted 11 lines · Namhyung Kim

src/calibrate/calibrate.cpp static void *burn_disk(void *dummy) · line 299

Quoted 4 lines
src/calibrate/calibrate.cpp +2 -1
-		write(fd, buffer, 64*1024);
+		if (write(fd, buffer, 64*1024))
+			/* make gcc silent */;
  		fdatasync(fd);

I'm sorry, but no.
This is waaaaaaaaaaay worse than leaving the compiler warning.
The compiler warning is real, and is a reminder that we need to improve the code "just make it shut up" patches are a really bad idea, and this is the perfect example of that.

So how can we improve the code then?

Signature · 2 lines
Regards,
Namhyung Kim
#7 lore raw

-----Original Message-----
From: powertop-bounces(a)lists.01.org [mailto:powertop-bounces(a)lists.01.org] On Behalf Of Namhyung Kim
Sent: Monday, December 03, 2012 8:18 AM
To: Arjan van de Ven
Cc: powertop(a)lists.01.org
Subject: Re: [Powertop] [PATCH 1/3] Suppress compiler warnings on write()

Hi Arjan,

2012-12-03 (월), 08:14 -0800, Arjan van de Ven:

Quoted 11 lines · Namhyung Kim

src/calibrate/calibrate.cpp static void *burn_disk(void *dummy) · line 299

Quoted 4 lines
src/calibrate/calibrate.cpp +2 -1
-		write(fd, buffer, 64*1024);
+		if (write(fd, buffer, 64*1024))
+			/* make gcc silent */;
  		fdatasync(fd);

I'm sorry, but no.
This is waaaaaaaaaaay worse than leaving the compiler warning.
The compiler warning is real, and is a reminder that we need to
improve the code "just make it shut up" patches are a really bad idea, and this is the perfect example of that.

So how can we improve the code then?

The code needs to be refactored to handle the error. -c

--
Regards,
Namhyung Kim

_______________________________________________
PowerTop mailing list
PowerTop(a)lists.01.org
https://lists.01.org/mailman/listinfo/powertop