Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@chenbd
Copy link
Contributor

@chenbd chenbd commented Mar 30, 2019

The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some(immutable) static string (in which case buf is unused).

h2o_perror should handle this case.

@chenbd
Copy link
Contributor Author

chenbd commented Mar 30, 2019

@kazuho Would you please review this PR?
Is const char *h2o_strerror_r better?

@kazuho
Copy link
Member

kazuho commented Mar 31, 2019

Oh nice catch!

It's unfortunate that glibc behaves differently than what is defined in POSIX.

How about moving h2o_perror to the end of memory.c (also changing it to be a non-inlined function), that is defined after macros indicating glibc to use the POSIX-compliant version (i.e. (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE)?

I think that this is not a hot path, and I prefer having one code that gets executed on all platforms.

@chenbd
Copy link
Contributor Author

chenbd commented Apr 1, 2019

@kazuho Fixed, Moved to no inline functions.

Guess _GNU_SOURCE is enough for judge.

Also h2o_strerror_r is reserved and not removed, but i'm not sure whether const char * is better than void.

(*list)[cnt++] = element;
(*list)[cnt] = NULL;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes. To clarify, my suggestion is to do

#define _POSIX_C_SOURCE 200112L
#undef _GNU_SOURCE

here, so that the POSIX variant would be used below.

We can retain h2o_strerror_r returning void as-is.

Copy link
Contributor Author

@chenbd chenbd Apr 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the choice is using POSIX version instead of gnu version.
Should this be defined at the beginning of file before any #include (or at lease before #include <string.h>)?

If #define _POSIX_C_SOURCE 200112L and #undef _GNU_SOURCE
at the top of the file, there are some compiler warnings for getpagesize, alloca and mkstemp.
Seems not a very good solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chenbd I think you are correct. Using macros is not a good way to resolve the issue.

So it would either be calling __xpg_strerror_r (it's part of the linux ABI, see http://refspecs.linux-foundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/baselib-xpg-strerror_r-3.html), or creating a wrapper for strerror_r.

I think that your approach of creating a wrapper is better, and I think the wrapper API we should provide is the one that returns char *; it's theoretically faster and it's easier to mimic. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I prefer char *h2o_strerror_r(int errnum, char *buf, size_t buflen);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes. I believe you need to use the result of the function call here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry for my miss.
Fixed in 82b0800

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, Recently most systems implements strerror using thread local data.
so indeed, strerror and strerror_r should be both thread safe on these systems. but man page do not assure this.

all these strerror_r related seems historical reasons.

@kazuho
Copy link
Member

kazuho commented Apr 2, 2019

Thank you for all the work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants