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 @@
#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) {
for (auto p = ((const char*)s) + n - 1; p >= (const char*)s; p--) {
if (*p == (char)c) {
......@@ -27,23 +27,24 @@ extern "C" void* memrchr(const void* s, int c, size_t n) {
}
#endif
#ifdef _WIN32
#include <stdlib.h>
extern "C" {
char* strndup(const char* a, size_t len) {
#if defined(_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
extern "C" char* strndup(const char* a, size_t len) {
auto neededLen = strlen(a);
if (neededLen > len) {
neededLen = len - 1;
neededLen = len;
}
char* buf = (char*)malloc((neededLen + 1) * sizeof(char));
if (!buf) {
return nullptr;
}
memcpy(buf, a, neededLen);
buf[neededLen] = '\0';
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);
}
}
#endif
......@@ -17,17 +17,18 @@
#pragma once
#include <string.h>
#include <stdlib.h>
#include <folly/portability/Config.h>
#if !FOLLY_HAVE_MEMRCHR || defined(_WIN32) || defined(__APPLE__)
#include <stdint.h>
#if !FOLLY_HAVE_MEMRCHR
extern "C" void* memrchr(const void* s, int c, size_t n);
#endif
#if defined(_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
extern "C" char* strndup(const char* a, size_t len);
#endif
#ifdef _WIN32
extern "C" {
char* strndup(const char* a, size_t len);
char* strtok_r(char* str, char const* delim, char** ctx);
}
extern "C" char* strtok_r(char* str, char const* delim, char** ctx);
#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