Avoid strerror_r portability issues

R=johnmccutchan@google.com

Review URL: https://codereview.chromium.org/1450113003 .
This commit is contained in:
John McCutchan 2015-11-17 15:23:47 -08:00
parent ec994696df
commit ca81c1732b
18 changed files with 68 additions and 51 deletions

View file

@ -90,7 +90,7 @@ void DebuggerConnectionImpl::HandleEvent(struct kevent* event) {
if (status == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("Failed adding debugger socket to kqueue: %s\n", error_message);
}
*/
@ -117,7 +117,7 @@ void DebuggerConnectionImpl::Handler(uword args) {
if (result == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("kevent failed %s\n", error_message);
} else {
ASSERT(result <= kMaxEvents);
@ -149,7 +149,7 @@ void DebuggerConnectionImpl::SetupPollQueue() {
if (status == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("Failed adding wakeup pipe fd to kqueue: %s\n", error_message);
}
@ -160,7 +160,7 @@ void DebuggerConnectionImpl::SetupPollQueue() {
if (status == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("Failed adding listener socket to kqueue: %s\n", error_message);
}
}

View file

@ -132,7 +132,7 @@ EventHandlerImplementation::EventHandlerImplementation()
if (status == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("Failed adding interrupt fd to kqueue: %s\n", error_message);
}
}
@ -358,7 +358,7 @@ void EventHandlerImplementation::HandleEvents(struct kevent* events,
if ((events[i].flags & EV_ERROR) != 0) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(events[i].data, error_message, kBufferSize);
Utils::StrError(events[i].data, error_message, kBufferSize);
FATAL1("kevent failed %s\n", error_message);
}
if (events[i].udata == NULL) {
@ -440,7 +440,7 @@ void EventHandlerImplementation::EventHandlerEntry(uword args) {
if (result == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL1("kevent failed %s\n", error_message);
} else {
handler_impl->HandleTimeout();

View file

@ -19,6 +19,7 @@
#include "bin/log.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
namespace dart {
@ -57,7 +58,7 @@ void File::Close() {
if (err != 0) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
Log::PrintErr("%s\n", error_message);
}
}
@ -422,7 +423,7 @@ File::StdioHandleType File::GetStdioHandleType(int fd) {
if (result == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL2("Failed stat on file descriptor %d: %s", fd, error_message);
}
if (S_ISCHR(buf.st_mode)) return kTerminal;

View file

@ -15,9 +15,10 @@
#include <unistd.h> // NOLINT
#include <libgen.h> // NOLINT
#include "platform/signal_blocker.h"
#include "bin/builtin.h"
#include "bin/log.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
namespace dart {
@ -56,7 +57,7 @@ void File::Close() {
if (err != 0) {
const int kBufferSize = 1024;
char error_buf[kBufferSize];
Log::PrintErr("%s\n", strerror_r(errno, error_buf, kBufferSize));
Log::PrintErr("%s\n", Utils::StrError(errno, error_buf, kBufferSize));
}
}
handle_->set_fd(kClosedFd);
@ -429,7 +430,7 @@ File::StdioHandleType File::GetStdioHandleType(int fd) {
const int kBufferSize = 1024;
char error_buf[kBufferSize];
FATAL2("Failed stat on file descriptor %d: %s", fd,
strerror_r(errno, error_buf, kBufferSize));
Utils::StrError(errno, error_buf, kBufferSize));
}
if (S_ISCHR(buf.st_mode)) return kTerminal;
if (S_ISFIFO(buf.st_mode)) return kPipe;

View file

@ -20,6 +20,7 @@
#include "bin/log.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
@ -57,7 +58,7 @@ void File::Close() {
if (err != 0) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
Log::PrintErr("%s\n", error_message);
}
}
@ -394,7 +395,7 @@ File::StdioHandleType File::GetStdioHandleType(int fd) {
if (result == -1) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
FATAL2("Failed stat on file descriptor %d: %s", fd, error_message);
}
if (S_ISCHR(buf.st_mode)) return kTerminal;

View file

@ -22,6 +22,7 @@
#include "bin/thread.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
extern char **environ;
@ -644,7 +645,7 @@ class ProcessStarter {
void SetChildOsErrorMessage() {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
*os_error_message_ = strdup(error_message);
}
@ -655,7 +656,7 @@ class ProcessStarter {
int child_errno = errno;
const int kBufferSize = 1024;
char os_error_message[kBufferSize];
strerror_r(errno, os_error_message, kBufferSize);
Utils::StrError(errno, os_error_message, kBufferSize);
int bytes_written =
FDUtils::WriteToBlocking(
exec_control_[1], &child_errno, sizeof(child_errno));

View file

@ -16,11 +16,12 @@
#include <sys/wait.h> // NOLINT
#include <unistd.h> // NOLINT
#include "platform/signal_blocker.h"
#include "bin/fdutils.h"
#include "bin/lockers.h"
#include "bin/log.h"
#include "bin/thread.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
extern char **environ;
@ -643,7 +644,7 @@ class ProcessStarter {
void SetChildOsErrorMessage() {
const int kBufferSize = 1024;
char error_buf[kBufferSize];
*os_error_message_ = strdup(strerror_r(errno, error_buf, kBufferSize));
*os_error_message_ = strdup(Utils::StrError(errno, error_buf, kBufferSize));
}
@ -653,7 +654,7 @@ class ProcessStarter {
int child_errno = errno;
const int kBufferSize = 1024;
char error_buf[kBufferSize];
char* os_error_message = strerror_r(errno, error_buf, kBufferSize);
char* os_error_message = Utils::StrError(errno, error_buf, kBufferSize);
int bytes_written =
FDUtils::WriteToBlocking(
exec_control_[1], &child_errno, sizeof(child_errno));

View file

@ -25,6 +25,7 @@
#include "bin/thread.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
@ -660,7 +661,7 @@ class ProcessStarter {
void SetChildOsErrorMessage() {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
*os_error_message_ = strdup(error_message);
}
@ -671,7 +672,7 @@ class ProcessStarter {
int child_errno = errno;
const int kBufferSize = 1024;
char os_error_message[kBufferSize];
strerror_r(errno, os_error_message, kBufferSize);
Utils::StrError(errno, os_error_message, kBufferSize);
int bytes_written =
FDUtils::WriteToBlocking(
exec_control_[1], &child_errno, sizeof(child_errno));

View file

@ -11,6 +11,7 @@
#include <sys/time.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
@ -19,7 +20,7 @@ namespace bin {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
FATAL2("pthread error: %d (%s)", result, error_message); \
}
@ -29,7 +30,7 @@ namespace bin {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, error_message); \
return result; \

View file

@ -12,6 +12,7 @@
#include <sys/time.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
@ -21,7 +22,7 @@ namespace bin {
const int kBufferSize = 1024; \
char error_buf[kBufferSize]; \
FATAL2("pthread error: %d (%s)", result, \
strerror_r(result, error_buf, kBufferSize)); \
Utils::StrError(result, error_buf, kBufferSize)); \
}
@ -32,7 +33,7 @@ namespace bin {
char error_buf[kBufferSize]; \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, \
strerror_r(result, error_buf, kBufferSize)); \
Utils::StrError(result, error_buf, kBufferSize)); \
return result; \
}
#else

View file

@ -19,6 +19,7 @@
#include <mach/thread_act.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
namespace bin {
@ -27,7 +28,7 @@ namespace bin {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
FATAL2("pthread error: %d (%s)", result, error_message); \
}
@ -37,7 +38,7 @@ namespace bin {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, error_message); \
return result; \

View file

@ -12,6 +12,7 @@
#include "bin/utils.h"
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
@ -22,7 +23,7 @@ OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
set_code(errno);
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
SetMessage(error_message);
}
@ -33,7 +34,7 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
if (sub_system == kSystem) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(code, error_message, kBufferSize);
Utils::StrError(code, error_message, kBufferSize);
SetMessage(error_message);
} else if (sub_system == kGetAddressInfo) {
SetMessage(gai_strerror(code));

View file

@ -12,6 +12,7 @@
#include "bin/utils.h"
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
@ -22,7 +23,7 @@ OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
set_code(errno);
const int kBufferSize = 1024;
char error_buf[kBufferSize];
SetMessage(strerror_r(errno, error_buf, kBufferSize));
SetMessage(Utils::StrError(errno, error_buf, kBufferSize));
}
@ -32,7 +33,7 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
if (sub_system == kSystem) {
const int kBufferSize = 1024;
char error_buf[kBufferSize];
SetMessage(strerror_r(code, error_buf, kBufferSize));
SetMessage(Utils::StrError(code, error_buf, kBufferSize));
} else if (sub_system == kGetAddressInfo) {
SetMessage(gai_strerror(code));
} else {

View file

@ -12,6 +12,7 @@
#include "bin/utils.h"
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
@ -22,7 +23,7 @@ OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
set_code(errno);
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(errno, error_message, kBufferSize);
Utils::StrError(errno, error_message, kBufferSize);
SetMessage(error_message);
}
@ -33,7 +34,7 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
if (sub_system == kSystem) {
const int kBufferSize = 1024;
char error_message[kBufferSize];
strerror_r(code, error_message, kBufferSize);
Utils::StrError(code, error_message, kBufferSize);
SetMessage(error_message);
} else if (sub_system == kGetAddressInfo) {
SetMessage(gai_strerror(code));

View file

@ -212,6 +212,20 @@ class Utils {
return ((-0x20000000000000LL <= value) && (value <= 0x20000000000000LL));
#else
return true;
#endif
}
static char* StrError(int err, char* buffer, size_t bufsize) {
#if !defined(__GLIBC__) || \
((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
// Use the standard version
if (strerror_r(err, buffer, bufsize) != 0) {
snprintf(buffer, bufsize, "%s", "strerror_r failed");
}
return buffer;
#else
// Use the gnu specific version
return strerror_r(err, buffer, bufsize);
#endif
}
};

View file

@ -11,6 +11,7 @@
#include <sys/time.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
@ -18,7 +19,7 @@ namespace dart {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
FATAL2("pthread error: %d (%s)", result, error_message); \
}
@ -36,7 +37,7 @@ namespace dart {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, error_message); \
return result; \

View file

@ -13,27 +13,16 @@
#include <sys/time.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
static char* strerror_helper(int err, char* buffer, size_t bufsize) {
#if !defined(__GLIBC__) || \
((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
// Use the standard version
return strerror_r(err, buffer, bufsize) == 0 ?
buffer : const_cast<char*>("strerror_r failed");
#else
// Use the gnu specific version
return strerror_r(err, buffer, bufsize);
#endif
}
#define VALIDATE_PTHREAD_RESULT(result) \
if (result != 0) { \
const int kBufferSize = 1024; \
char error_buf[kBufferSize]; \
FATAL2("pthread error: %d (%s)", result, \
strerror_helper(result, error_buf, kBufferSize)); \
Utils::StrError(result, error_buf, kBufferSize)); \
}
@ -52,7 +41,7 @@ static char* strerror_helper(int err, char* buffer, size_t bufsize) {
char error_buf[kBufferSize]; \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, \
strerror_helper(result, error_buf, kBufferSize)); \
Utils::StrError(result, error_buf, kBufferSize)); \
return result; \
}
#else

View file

@ -19,6 +19,7 @@
#include <mach/thread_act.h> // NOLINT
#include "platform/assert.h"
#include "platform/utils.h"
namespace dart {
@ -26,7 +27,7 @@ namespace dart {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
FATAL2("pthread error: %d (%s)", result, error_message); \
}
@ -44,7 +45,7 @@ namespace dart {
if (result != 0) { \
const int kBufferSize = 1024; \
char error_message[kBufferSize]; \
strerror_r(result, error_message, kBufferSize); \
Utils::StrError(result, error_message, kBufferSize); \
fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \
__FILE__, __LINE__, result, error_message); \
return result; \