Skip to contentSkip to conversation

event-parse: Add compatibility for non GNU strerror_r

PATCH In review Rosen Penev 2 participants 3 messages Jun 4, 2019 – Jun 7, 2019

lore
Share & tools
lore

Rosen Penev

author
#0 lore raw

PATCH 1/1 event-parse: Add compatibility for non GNU strerror_r

Needed for libraries like musl that do not implement the GNU variant.

Signed-off-by Rosen Penev <rosenp(a)gmail.com>
---
 traceevent/event-parse.c | 5 +++++
 1 file changed, 5 insertions(+)
traceevent/event-parse.c +5 -0
index 5a717a0..1f7eb80 100644
@@ -5121,12 +5121,17 @@ int pevent_strerror(struct pevent *pevent __maybe_unused,
 	const char *msg;
 
 	if (errnum >= 0) {
+#if defined(__GLIBC__)
 		msg = strerror_r(errnum, buf, buflen);
 		if (msg != buf) {
 			size_t len = strlen(msg);
 			memcpy(buf, msg, min(buflen - 1, len));
 			*(buf + min(buflen - 1, len)) = '\0';
 		}
+#else
+		if (strerror_r(errnum, buf, buflen))
+			snprintf(buf, buflen, "errnum %i", errnum);
+#endif
 		return 0;
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 1 line · Rosen Penev

Needed for libraries like musl that do not implement the GNU variant.

This patch seems curious.

Quoted 13 lines
Signed-off-by Rosen Penev <rosenp(a)gmail.com>
---
 traceevent/event-parse.c | 5 +++++
 1 file changed, 5 insertions(+)
traceevent/event-parse.c +0 -0
index 5a717a0..1f7eb80 100644
@@ -5121,12 +5121,17 @@ int pevent_strerror(struct pevent *pevent __maybe_unused,
 	const char *msg;
 
 	if (errnum >= 0) {

traceevent/event-parse.c int pevent_strerror(struct pevent *pevent __maybe_unused, · line 5133

Quoted 10 lines
traceevent/event-parse.c +4 -0
+#if defined(__GLIBC__)
 		msg = strerror_r(errnum, buf, buflen);
 		if (msg != buf) {
 			size_t len = strlen(msg);
 			memcpy(buf, msg, min(buflen - 1, len));
 			*(buf + min(buflen - 1, len)) = '\0';
 		}
+#else
+		if (strerror_r(errnum, buf, buflen))
+			snprintf(buf, buflen, "errnum %i", errnum);

Was

if (a human readable response is returned from strerror_r)
clobber it with a numeric version of the error

really what you wanted to achieve?

I would rather expect something like

	if (errnum >= 0) {
		msg = strerror_r(errnum, buf, buflen);
#if defined(__GLIBC__)
		if (msg != buf)
			strlcpy(buf, msg, buflen);
	#endif
	}

where the #ifdef ends up as a pure optimization if strerror_r is the XSI version.

/MF

Quoted 11 lines
traceevent/event-parse.c +1 -0
+#endif
 		return 0;
 	}
 
Signature · 6 lines
2.17.1

_______________________________________________
PowerTop mailing list
PowerTop(a)lists.01.org
https://lists.01.org/mailman/listinfo/powertop
author
#2 lore raw
Quoted 32 lines · Magnus Fromreide
Quoted 1 line · Rosen Penev

Needed for libraries like musl that do not implement the GNU variant.

This patch seems curious.

Quoted 13 lines
Signed-off-by Rosen Penev <rosenp(a)gmail.com>
---
  traceevent/event-parse.c | 5 +++++
  1 file changed, 5 insertions(+)
traceevent/event-parse.c +0 -0
index 5a717a0..1f7eb80 100644
@@ -5121,12 +5121,17 @@ int pevent_strerror(struct pevent *pevent __maybe_unused,
  	const char *msg;
 
  	if (errnum >= 0) {

traceevent/event-parse.c int pevent_strerror(struct pevent *pevent __maybe_unused, · line 5133

Quoted 10 lines
traceevent/event-parse.c +4 -0
+#if defined(__GLIBC__)
  		msg = strerror_r(errnum, buf, buflen);
  		if (msg != buf) {
  			size_t len = strlen(msg);
  			memcpy(buf, msg, min(buflen - 1, len));
  			*(buf + min(buflen - 1, len)) = '\0';
  		}
+#else
+		if (strerror_r(errnum, buf, buflen))
+			snprintf(buf, buflen, "errnum %i", errnum);

Was

if (a human readable response is returned from strerror_r)
clobber it with a numeric version of the error

Quoted 6 lines

really what you wanted to achieve?

I would rather expect something like

	if (errnum >= 0) {
		msg = strerror_r(errnum, buf, buflen);

No idea what you mean by this. The XSI strerror_r returns an int, not a char pointer.

Quoted 21 lines
#if defined(__GLIBC__)
		if (msg != buf)
			strlcpy(buf, msg, buflen);
	#endif
	}

where the #ifdef ends up as a pure optimization if strerror_r is the XSI version.

/MF

Quoted 11 lines
traceevent/event-parse.c +1 -0
+#endif
  		return 0;
  	}
 
Signature · 6 lines
2.17.1

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