Use non-deprecated methods on windows.

Use of strdup, close, lseek, read, and write prevent linking
statically againt libcmt.lib.

Change-Id: I04f7876ec0f03f29f000bbcc6b2ccdec844452d2
Reviewed-on: https://boringssl-review.googlesource.com/8010
Reviewed-by: David Benjamin <davidben@google.com>
This commit is contained in:
nmittler 2016-05-19 08:49:59 -07:00 committed by David Benjamin
parent e09e579603
commit f0322b2abc
8 changed files with 54 additions and 30 deletions

View File

@ -74,8 +74,6 @@ elseif(MSVC)
"C4800" # 'int' : forcing value to bool 'true' or 'false' "C4800" # 'int' : forcing value to bool 'true' or 'false'
# (performance warning) # (performance warning)
"C4820" # 'bytes' bytes padding added after construct 'member_name' "C4820" # 'bytes' bytes padding added after construct 'member_name'
"C4996" # 'read': The POSIX name for this item is deprecated. Instead,
# use the ISO C++ conformant name: _read.
"C5027" # move assignment operator was implicitly defined as deleted "C5027" # move assignment operator was implicitly defined as deleted
) )
set(MSVC_LEVEL4_WARNINGS_LIST set(MSVC_LEVEL4_WARNINGS_LIST
@ -91,6 +89,7 @@ elseif(MSVC)
add_definitions(-D_HAS_EXCEPTIONS=0) add_definitions(-D_HAS_EXCEPTIONS=0)
add_definitions(-DWIN32_LEAN_AND_MEAN) add_definitions(-DWIN32_LEAN_AND_MEAN)
add_definitions(-DNOMINMAX) add_definitions(-DNOMINMAX)
add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
endif() endif()
if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR

View File

@ -108,20 +108,25 @@ static int bio_fd_non_fatal_error(int err) {
} }
#if defined(OPENSSL_WINDOWS) #if defined(OPENSSL_WINDOWS)
int bio_fd_should_retry(int i) { #define BORINGSSL_ERRNO (int)GetLastError()
if (i == -1) { #define BORINGSSL_CLOSE _close
return bio_fd_non_fatal_error((int)GetLastError()); #define BORINGSSL_LSEEK _lseek
} #define BORINGSSL_READ _read
return 0; #define BORINGSSL_WRITE _write
}
#else #else
#define BORINGSSL_ERRNO errno
#define BORINGSSL_CLOSE close
#define BORINGSSL_LSEEK lseek
#define BORINGSSL_READ read
#define BORINGSSL_WRITE write
#endif
int bio_fd_should_retry(int i) { int bio_fd_should_retry(int i) {
if (i == -1) { if (i == -1) {
return bio_fd_non_fatal_error(errno); return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
} }
return 0; return 0;
} }
#endif
BIO *BIO_new_fd(int fd, int close_flag) { BIO *BIO_new_fd(int fd, int close_flag) {
BIO *ret = BIO_new(BIO_s_fd()); BIO *ret = BIO_new(BIO_s_fd());
@ -145,7 +150,7 @@ static int fd_free(BIO *bio) {
if (bio->shutdown) { if (bio->shutdown) {
if (bio->init) { if (bio->init) {
close(bio->num); BORINGSSL_CLOSE(bio->num);
} }
bio->init = 0; bio->init = 0;
} }
@ -155,7 +160,7 @@ static int fd_free(BIO *bio) {
static int fd_read(BIO *b, char *out, int outl) { static int fd_read(BIO *b, char *out, int outl) {
int ret = 0; int ret = 0;
ret = read(b->num, out, outl); ret = BORINGSSL_READ(b->num, out, outl);
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
if (ret <= 0) { if (ret <= 0) {
if (bio_fd_should_retry(ret)) { if (bio_fd_should_retry(ret)) {
@ -167,7 +172,7 @@ static int fd_read(BIO *b, char *out, int outl) {
} }
static int fd_write(BIO *b, const char *in, int inl) { static int fd_write(BIO *b, const char *in, int inl) {
int ret = write(b->num, in, inl); int ret = BORINGSSL_WRITE(b->num, in, inl);
BIO_clear_retry_flags(b); BIO_clear_retry_flags(b);
if (ret <= 0) { if (ret <= 0) {
if (bio_fd_should_retry(ret)) { if (bio_fd_should_retry(ret)) {
@ -188,14 +193,14 @@ static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
case BIO_C_FILE_SEEK: case BIO_C_FILE_SEEK:
ret = 0; ret = 0;
if (b->init) { if (b->init) {
ret = (long)lseek(b->num, num, SEEK_SET); ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
} }
break; break;
case BIO_C_FILE_TELL: case BIO_C_FILE_TELL:
case BIO_CTRL_INFO: case BIO_CTRL_INFO:
ret = 0; ret = 0;
if (b->init) { if (b->init) {
ret = (long)lseek(b->num, 0, SEEK_CUR); ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
} }
break; break;
case BIO_C_SET_FD: case BIO_C_SET_FD:

View File

@ -152,7 +152,11 @@ int main(int argc, char **argv) {
case 1: case 1:
s = rand_string(); s = rand_string();
lh_insert(lh, (void **)&s1, s); lh_insert(lh, (void **)&s1, s);
#if defined(OPENSSL_WINDOWS)
dummy_lh_insert(&dummy_lh, &s2, _strdup(s));
#else
dummy_lh_insert(&dummy_lh, &s2, strdup(s)); dummy_lh_insert(&dummy_lh, &s2, strdup(s));
#endif
if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) { if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
fprintf(stderr, "lh_insert failure\n"); fprintf(stderr, "lh_insert failure\n");

View File

@ -147,8 +147,6 @@ uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
return h; return h;
} }
char *OPENSSL_strdup(const char *s) { return strdup(s); }
size_t OPENSSL_strnlen(const char *s, size_t len) { size_t OPENSSL_strnlen(const char *s, size_t len) {
size_t i; size_t i;
@ -163,6 +161,8 @@ size_t OPENSSL_strnlen(const char *s, size_t len) {
#if defined(OPENSSL_WINDOWS) #if defined(OPENSSL_WINDOWS)
char *OPENSSL_strdup(const char *s) { return _strdup(s); }
int OPENSSL_strcasecmp(const char *a, const char *b) { int OPENSSL_strcasecmp(const char *a, const char *b) {
return _stricmp(a, b); return _stricmp(a, b);
} }
@ -173,6 +173,8 @@ int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
#else #else
char *OPENSSL_strdup(const char *s) { return strdup(s); }
int OPENSSL_strcasecmp(const char *a, const char *b) { int OPENSSL_strcasecmp(const char *a, const char *b) {
return strcasecmp(a, b); return strcasecmp(a, b);
} }

View File

@ -46,7 +46,7 @@ typedef int ssize_t;
struct close_delete { struct close_delete {
void operator()(int *fd) { void operator()(int *fd) {
close(*fd); BORINGSSL_CLOSE(*fd);
} }
}; };
@ -83,7 +83,7 @@ static const char kStdinName[] = "standard input";
static bool OpenFile(int *out_fd, const std::string &filename) { static bool OpenFile(int *out_fd, const std::string &filename) {
*out_fd = -1; *out_fd = -1;
int fd = open(filename.c_str(), O_RDONLY | O_BINARY); int fd = BORINGSSL_OPEN(filename.c_str(), O_RDONLY | O_BINARY);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "Failed to open input file '%s': %s\n", filename.c_str(), fprintf(stderr, "Failed to open input file '%s': %s\n", filename.c_str(),
strerror(errno)); strerror(errno));
@ -146,7 +146,7 @@ static bool SumFile(std::string *out_hex, const EVP_MD *md,
ssize_t n; ssize_t n;
do { do {
n = read(fd, buf.get(), kBufSize); n = BORINGSSL_READ(fd, buf.get(), kBufSize);
} while (n == -1 && errno == EINTR); } while (n == -1 && errno == EINTR);
if (n == 0) { if (n == 0) {
@ -234,10 +234,10 @@ static bool Check(const CheckModeArguments &args, const EVP_MD *md,
return false; return false;
} }
file = fdopen(fd, "rb"); file = BORINGSSL_FDOPEN(fd, "rb");
if (!file) { if (!file) {
perror("fdopen"); perror("fdopen");
close(fd); BORINGSSL_CLOSE(fd);
return false; return false;
} }

View File

@ -34,6 +34,20 @@
#pragma warning(pop) #pragma warning(pop)
#endif #endif
#if defined(OPENSSL_WINDOWS)
#define BORINGSSL_OPEN _open
#define BORINGSSL_FDOPEN _fdopen
#define BORINGSSL_CLOSE _close
#define BORINGSSL_READ _read
#define BORINGSSL_WRITE _write
#else
#define BORINGSSL_OPEN open
#define BORINGSSL_FDOPEN fdopen
#define BORINGSSL_CLOSE close
#define BORINGSSL_READ read
#define BORINGSSL_WRITE write
#endif
enum ArgumentType { enum ArgumentType {
kRequiredArgument, kRequiredArgument,
kOptionalArgument, kOptionalArgument,

View File

@ -64,7 +64,7 @@ bool DoPKCS12(const std::vector<std::string> &args) {
return false; return false;
} }
int fd = open(args_map["-dump"].c_str(), O_RDONLY); int fd = BORINGSSL_OPEN(args_map["-dump"].c_str(), O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("open"); perror("open");
return false; return false;
@ -73,7 +73,7 @@ bool DoPKCS12(const std::vector<std::string> &args) {
struct stat st; struct stat st;
if (fstat(fd, &st)) { if (fstat(fd, &st)) {
perror("fstat"); perror("fstat");
close(fd); BORINGSSL_CLOSE(fd);
return false; return false;
} }
const size_t size = st.st_size; const size_t size = st.st_size;
@ -82,7 +82,7 @@ bool DoPKCS12(const std::vector<std::string> &args) {
read_result_t n; read_result_t n;
size_t off = 0; size_t off = 0;
do { do {
n = read(fd, &contents[off], size - off); n = BORINGSSL_READ(fd, &contents[off], size - off);
if (n >= 0) { if (n >= 0) {
off += static_cast<size_t>(n); off += static_cast<size_t>(n);
} }
@ -90,11 +90,11 @@ bool DoPKCS12(const std::vector<std::string> &args) {
if (off != size) { if (off != size) {
perror("read"); perror("read");
close(fd); BORINGSSL_CLOSE(fd);
return false; return false;
} }
close(fd); BORINGSSL_CLOSE(fd);
printf("Enter password: "); printf("Enter password: ");
fflush(stdout); fflush(stdout);
@ -102,7 +102,7 @@ bool DoPKCS12(const std::vector<std::string> &args) {
char password[256]; char password[256];
off = 0; off = 0;
do { do {
n = read(0, &password[off], sizeof(password) - 1 - off); n = BORINGSSL_READ(0, &password[off], sizeof(password) - 1 - off);
if (n >= 0) { if (n >= 0) {
off += static_cast<size_t>(n); off += static_cast<size_t>(n);
} }

View File

@ -265,7 +265,7 @@ bool TransferData(SSL *ssl, int sock) {
ssize_t n; ssize_t n;
do { do {
n = read(0, buffer, sizeof(buffer)); n = BORINGSSL_READ(0, buffer, sizeof(buffer));
} while (n == -1 && errno == EINTR); } while (n == -1 && errno == EINTR);
if (n == 0) { if (n == 0) {
@ -319,7 +319,7 @@ bool TransferData(SSL *ssl, int sock) {
ssize_t n; ssize_t n;
do { do {
n = write(1, buffer, ssl_ret); n = BORINGSSL_WRITE(1, buffer, ssl_ret);
} while (n == -1 && errno == EINTR); } while (n == -1 && errno == EINTR);
if (n != ssl_ret) { if (n != ssl_ret) {