Commit e3774034 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot 8

Fix the portability implementation of strndup

Summary:It was mistakenly assuming the length passed in included the null terminator.
This also makes the portability implementation of `strndup` available to OSX and FreeBSD, where they weren't present, and where HHVM had a wrapper for them.
This also removes the extra pair of conditions around `memrchr`, as the main define should always be getting set.

Reviewed By: yfeldblum

Differential Revision: D3116467

fb-gh-sync-id: 243dd4dace219efab2c2bf2f383202e70fbec4de
fbshipit-source-id: 243dd4dace219efab2c2bf2f383202e70fbec4de
parent 10001685
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include <folly/portability/String.h> #include <folly/portability/String.h>
#if !FOLLY_HAVE_MEMRCHR || defined(_WIN32) || defined(__APPLE__) #if !FOLLY_HAVE_MEMRCHR
extern "C" void* memrchr(const void* s, int c, size_t n) { extern "C" void* memrchr(const void* s, int c, size_t n) {
for (auto p = ((const char*)s) + n - 1; p >= (const char*)s; p--) { for (auto p = ((const char*)s) + n - 1; p >= (const char*)s; p--) {
if (*p == (char)c) { if (*p == (char)c) {
...@@ -27,23 +27,24 @@ extern "C" void* memrchr(const void* s, int c, size_t n) { ...@@ -27,23 +27,24 @@ extern "C" void* memrchr(const void* s, int c, size_t n) {
} }
#endif #endif
#ifdef _WIN32 #if defined(_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
#include <stdlib.h> extern "C" char* strndup(const char* a, size_t len) {
extern "C" {
char* strndup(const char* a, size_t len) {
auto neededLen = strlen(a); auto neededLen = strlen(a);
if (neededLen > len) { if (neededLen > len) {
neededLen = len - 1; neededLen = len;
} }
char* buf = (char*)malloc((neededLen + 1) * sizeof(char)); char* buf = (char*)malloc((neededLen + 1) * sizeof(char));
if (!buf) {
return nullptr;
}
memcpy(buf, a, neededLen); memcpy(buf, a, neededLen);
buf[neededLen] = '\0'; buf[neededLen] = '\0';
return buf; return buf;
} }
#endif
char* strtok_r(char* str, char const* delim, char** ctx) { #ifdef _WIN32
extern "C" char* strtok_r(char* str, char const* delim, char** ctx) {
return strtok_s(str, delim, ctx); return strtok_s(str, delim, ctx);
} }
}
#endif #endif
...@@ -17,17 +17,18 @@ ...@@ -17,17 +17,18 @@
#pragma once #pragma once
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <folly/portability/Config.h> #include <folly/portability/Config.h>
#if !FOLLY_HAVE_MEMRCHR || defined(_WIN32) || defined(__APPLE__) #if !FOLLY_HAVE_MEMRCHR
#include <stdint.h>
extern "C" void* memrchr(const void* s, int c, size_t n); extern "C" void* memrchr(const void* s, int c, size_t n);
#endif #endif
#if defined(_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
extern "C" char* strndup(const char* a, size_t len);
#endif
#ifdef _WIN32 #ifdef _WIN32
extern "C" { extern "C" char* strtok_r(char* str, char const* delim, char** ctx);
char* strndup(const char* a, size_t len);
char* strtok_r(char* str, char const* delim, char** ctx);
}
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment