THRIFT-3873: fix various compiler warnings and overflow errors
THRIFT-3847: change VERSION to PACKAGE_VERSION to avoid conflicts with third party or OS headers
This closes #1128
diff --git a/build/cmake/ConfigureChecks.cmake b/build/cmake/ConfigureChecks.cmake
index f650544..81223d8 100644
--- a/build/cmake/ConfigureChecks.cmake
+++ b/build/cmake/ConfigureChecks.cmake
@@ -17,17 +17,16 @@
# under the License.
#
-
-include(CheckSymbolExists)
+include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckIncludeFiles)
-include(CheckFunctionExists)
+include(CheckSymbolExists)
-# If AI_ADDRCONFIG is not defined we define it as 0
-check_symbol_exists(AI_ADDRCONFIG "sys/types.h;sys/socket.h;netdb.h" HAVE_AI_ADDRCONFIG)
-if(NOT HAVE_AI_ADDRCONFIG)
-set(AI_ADDRCONFIG 1)
-endif(NOT HAVE_AI_ADDRCONFIG)
+if (Inttypes_FOUND)
+ # This allows the inttypes.h and stdint.h checks to succeed on platforms that
+ # do not natively provide there.
+ set (CMAKE_REQUIRED_INCLUDES ${INTTYPES_INCLUDE_DIRS})
+endif ()
check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
check_include_file(fcntl.h HAVE_FCNTL_H)
@@ -72,5 +71,5 @@
# generate a config.h file
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/build/cmake/config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/thrift/config.h")
-# HACK: Some files include thrift/config.h and some config.h so we include both. This should be cleaned up.
-include_directories("${CMAKE_CURRENT_BINARY_DIR}/thrift" "${CMAKE_CURRENT_BINARY_DIR}")
+
+include_directories("${CMAKE_CURRENT_BINARY_DIR}")
diff --git a/build/cmake/DefinePlatformSpecifc.cmake b/build/cmake/DefinePlatformSpecifc.cmake
index e57ecc2..496134c 100644
--- a/build/cmake/DefinePlatformSpecifc.cmake
+++ b/build/cmake/DefinePlatformSpecifc.cmake
@@ -71,12 +71,24 @@
message (FATAL_ERROR "Windows build does not support shared library output yet, please set -DWITH_SHARED_LIB=off")
endif()
+ add_definitions("/MP") # parallel build
+ add_definitions("/W3") # warning level 3
+
+ # VS2010 does not provide inttypes which we need for "PRId64" used in many places
+ find_package(Inttypes)
+ if (Inttypes_FOUND)
+ include_directories(${INTTYPES_INCLUDE_DIRS})
+ # OpenSSL conflicts with the definition of PRId64 unless it is defined first
+ add_definitions("/FIinttypes.h")
+ endif ()
elseif(UNIX)
find_program( MEMORYCHECK_COMMAND valgrind )
set( MEMORYCHECK_COMMAND_OPTIONS "--gen-suppressions=all --leak-check=full" )
set( MEMORYCHECK_SUPPRESSIONS_FILE "${PROJECT_SOURCE_DIR}/test/valgrind.suppress" )
endif()
+add_definitions("-D__STDC_FORMAT_MACROS")
+
# WITH_*THREADS selects which threading library to use
if(WITH_BOOSTTHREADS)
add_definitions("-DUSE_BOOST_THREAD=1")
diff --git a/build/cmake/FindInttypes.cmake b/build/cmake/FindInttypes.cmake
new file mode 100644
index 0000000..e661f78
--- /dev/null
+++ b/build/cmake/FindInttypes.cmake
@@ -0,0 +1,41 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# find msinttypes on compilers that don't provide it, for example
+# VS2010
+
+# Usage:
+# Provide INTTYPES_ROOT if you need it
+# Result: INTTYPES_INCLUDE_DIRS, where to find inttypes.h
+# Result: Inttypes_FOUND, If false, inttypes.h was not found
+
+find_path(INTTYPES_INCLUDE_DIRS inttypes.h HINTS ${INTTYPES_ROOT})
+if (INTTYPES_INCLUDE_DIRS)
+ set(Inttypes_FOUND TRUE)
+else ()
+ set(Inttypes_FOUND FALSE)
+ if (Inttypes_FIND_REQUIRED)
+ message(FATAL_ERROR "Could NOT find inttypes.h")
+ endif ()
+ message(STATUS "inttypes.h NOT found")
+endif ()
+
+mark_as_advanced(
+ INTTYPES_INCLUDE_DIRS
+)
diff --git a/build/cmake/FindLibevent.cmake b/build/cmake/FindLibevent.cmake
index 2bcd709..ac6a078 100644
--- a/build/cmake/FindLibevent.cmake
+++ b/build/cmake/FindLibevent.cmake
@@ -13,9 +13,13 @@
list(APPEND LibEvent_LIBRARIES_PATHS "${prefix}/lib")
endforeach()
-find_path(LIBEVENT_INCLUDE_DIRS event.h PATHS ${LibEvent_INCLUDE_PATHS})
-# "lib" prefix is needed on Windows
-find_library(LIBEVENT_LIBRARIES NAMES event libevent PATHS ${LibEvent_LIBRARIES_PATHS})
+# Looking for "event.h" will find the Platform SDK include dir on windows
+# so we also look for a peer header like evhttp.h to get the right path
+find_path(LIBEVENT_INCLUDE_DIRS evhttp.h event.h PATHS ${LibEvent_INCLUDE_PATHS})
+
+# "lib" prefix is needed on Windows in some cases
+# newer versions of libevent use three libraries
+find_library(LIBEVENT_LIBRARIES NAMES event event_core event_extra libevent PATHS ${LibEvent_LIBRARIES_PATHS})
if (LIBEVENT_LIBRARIES AND LIBEVENT_INCLUDE_DIRS)
set(Libevent_FOUND TRUE)
diff --git a/build/cmake/config.h.in b/build/cmake/config.h.in
index 181ea18..083bc55 100644
--- a/build/cmake/config.h.in
+++ b/build/cmake/config.h.in
@@ -44,9 +44,6 @@
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "${PACKAGE_STRING}"
-/* Version number of package */
-#define VERSION "${VERSION}"
-
/************************** DEFINES *************************/
/* Define if the AI_ADDRCONFIG symbol is unavailable */
@@ -154,4 +151,4 @@
/* Define to 1 if strerror_r returns char *. */
#cmakedefine STRERROR_R_CHAR_P 1
-#endif
\ No newline at end of file
+#endif
diff --git a/compiler/cpp/CMakeLists.txt b/compiler/cpp/CMakeLists.txt
index 85e351d..059c3bf 100644
--- a/compiler/cpp/CMakeLists.txt
+++ b/compiler/cpp/CMakeLists.txt
@@ -18,6 +18,14 @@
#
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/thrift/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/thrift/version.h)
+if(MSVC)
+ # The winflexbison generator outputs some macros that conflict with the Visual Studio 2010 copy of stdint.h
+ # This might be fixed in later versions of Visual Studio, but an easy solution is to include stdint.h first
+ if(HAVE_STDINT_H)
+ add_definitions(-D__STDC_LIMIT_MACROS)
+ add_definitions(/FI"stdint.h")
+ endif(HAVE_STDINT_H)
+endif()
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)
diff --git a/compiler/cpp/src/thrift/generate/t_erl_generator.cc b/compiler/cpp/src/thrift/generate/t_erl_generator.cc
index 4869414..6054a4e 100644
--- a/compiler/cpp/src/thrift/generate/t_erl_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_erl_generator.cc
@@ -20,6 +20,7 @@
#include <string>
#include <fstream>
#include <iostream>
+#include <limits>
#include <vector>
#include <stdlib.h>
@@ -968,10 +969,13 @@
}
void t_erl_generator::export_types_function(t_function* tfunction, string prefix) {
-
+ t_struct::members_type::size_type num = tfunction->get_arglist()->get_members().size();
+ if (num > static_cast<t_struct::members_type::size_type>(std::numeric_limits<int>().max())) {
+ throw "integer overflow in t_erl_generator::export_types_function, name " + tfunction->get_name();
+ }
export_types_string(prefix + tfunction->get_name(),
1 // This
- + ((tfunction->get_arglist())->get_members()).size());
+ + static_cast<int>(num));
}
void t_erl_generator::export_types_string(string name, int num) {
@@ -984,10 +988,13 @@
}
void t_erl_generator::export_function(t_function* tfunction, string prefix) {
-
+ t_struct::members_type::size_type num = tfunction->get_arglist()->get_members().size();
+ if (num > static_cast<t_struct::members_type::size_type>(std::numeric_limits<int>().max())) {
+ throw "integer overflow in t_erl_generator::export_function, name " + tfunction->get_name();
+ }
export_string(prefix + tfunction->get_name(),
1 // This
- + ((tfunction->get_arglist())->get_members()).size());
+ + static_cast<int>(num));
}
/**
diff --git a/compiler/cpp/src/thrift/generate/t_go_generator.cc b/compiler/cpp/src/thrift/generate/t_go_generator.cc
index defc682..bb5a609 100644
--- a/compiler/cpp/src/thrift/generate/t_go_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_go_generator.cc
@@ -25,9 +25,10 @@
* guide for ensuring uniformity and readability.
*/
-#include <string>
#include <fstream>
#include <iostream>
+#include <limits>
+#include <string>
#include <vector>
#include <stdlib.h>
@@ -433,7 +434,11 @@
if (islower(value2[i + 1])) {
value2.replace(i, 2, 1, toupper(value2[i + 1]));
}
- fix_common_initialism(value2, i);
+
+ if (i > static_cast<std::string::size_type>(std::numeric_limits<int>().max())) {
+ throw "integer overflow in t_go_generator::camelcase, value = " + value;
+ }
+ fix_common_initialism(value2, static_cast<int>(i));
}
}
@@ -2308,7 +2313,7 @@
f_remote << indent() << "}" << endl;
for (std::vector<t_field*>::size_type i = 0; i < num_args; ++i) {
- int flagArg = i + 1;
+ std::vector<t_field*>::size_type flagArg = i + 1;
t_type* the_type(args[i]->get_type());
t_type* the_type2(get_true_type(the_type));
diff --git a/compiler/cpp/src/thrift/thriftl.ll b/compiler/cpp/src/thrift/thriftl.ll
index bf7e8a5..30669a4 100644
--- a/compiler/cpp/src/thrift/thriftl.ll
+++ b/compiler/cpp/src/thrift/thriftl.ll
@@ -39,9 +39,15 @@
#endif
#ifdef _MSC_VER
-//warning C4102: 'find_rule' : unreferenced label
-#pragma warning(disable:4102)
-//avoid isatty redefinition
+#pragma warning( push )
+
+// warning C4102: 'find_rule' : unreferenced label
+#pragma warning( disable : 4102 )
+
+// warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
+#pragma warning( disable : 4267 )
+
+// avoid isatty redefinition
#define YY_NEVER_INTERACTIVE 1
#define YY_NO_UNISTD_H 1
@@ -459,5 +465,9 @@
%%
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif
+
/* vim: filetype=lex
*/
diff --git a/compiler/cpp/src/thrift/thrifty.yy b/compiler/cpp/src/thrift/thrifty.yy
index b7a7f72..fcbc877 100644
--- a/compiler/cpp/src/thrift/thrifty.yy
+++ b/compiler/cpp/src/thrift/thrifty.yy
@@ -25,8 +25,12 @@
*
*/
+#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
+#endif
+#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
+#endif
#include <stdio.h>
#ifndef _MSC_VER
#include <inttypes.h>
diff --git a/compiler/cpp/src/thrift/windows/config.h b/compiler/cpp/src/thrift/windows/config.h
index 5f057ca..d2269cf 100644
--- a/compiler/cpp/src/thrift/windows/config.h
+++ b/compiler/cpp/src/thrift/windows/config.h
@@ -34,9 +34,13 @@
#define strtoll(begin_ptr, end_ptr, length) _strtoi64(begin_ptr, end_ptr, length)
-#define PRIu64 "I64d"
-#define PRIi64 "I64d"
+#ifndef PRIu64
+#define PRIu64 "I64u"
+#endif
+#ifndef PRIi64
+#define PRIi64 "I64i"
+#endif
// squelch deprecation warnings
#pragma warning(disable : 4996)
// squelch bool conversion performance warning
diff --git a/lib/cpp/src/thrift/protocol/THeaderProtocol.h b/lib/cpp/src/thrift/protocol/THeaderProtocol.h
index e7d4bd6..b01bfb6 100644
--- a/lib/cpp/src/thrift/protocol/THeaderProtocol.h
+++ b/lib/cpp/src/thrift/protocol/THeaderProtocol.h
@@ -46,7 +46,7 @@
explicit THeaderProtocol(const boost::shared_ptr<TTransport>& trans,
uint16_t protoId = T_COMPACT_PROTOCOL)
: TVirtualProtocol<THeaderProtocol>(boost::shared_ptr<TTransport>(new THeaderTransport(trans))),
- trans_(boost::dynamic_pointer_cast<THeaderTransport>(this->getTransport())),
+ trans_(boost::dynamic_pointer_cast<THeaderTransport>(getTransport())),
protoId_(protoId) {
trans_->setProtocolId(protoId);
resetProtocol();
@@ -57,7 +57,7 @@
uint16_t protoId = T_COMPACT_PROTOCOL)
: TVirtualProtocol<THeaderProtocol>(
boost::shared_ptr<TTransport>(new THeaderTransport(inTrans, outTrans))),
- trans_(boost::dynamic_pointer_cast<THeaderTransport>(this->getTransport())),
+ trans_(boost::dynamic_pointer_cast<THeaderTransport>(getTransport())),
protoId_(protoId) {
trans_->setProtocolId(protoId);
resetProtocol();
diff --git a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
index d3ec722..431e7c4 100644
--- a/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
+++ b/lib/cpp/src/thrift/protocol/TJSONProtocol.cpp
@@ -524,7 +524,7 @@
std::string doubleToString(double d) {
std::ostringstream str;
str.imbue(std::locale::classic());
- const int max_digits10 = 2 + std::numeric_limits<double>::digits10;
+ const std::streamsize max_digits10 = 2 + std::numeric_limits<double>::digits10;
str.precision(max_digits10);
str << d;
return str.str();
diff --git a/lib/cpp/src/thrift/server/TNonblockingServer.cpp b/lib/cpp/src/thrift/server/TNonblockingServer.cpp
index ccc37a2..537770b 100644
--- a/lib/cpp/src/thrift/server/TNonblockingServer.cpp
+++ b/lib/cpp/src/thrift/server/TNonblockingServer.cpp
@@ -17,8 +17,6 @@
* under the License.
*/
-#define __STDC_FORMAT_MACROS
-
#include <thrift/thrift-config.h>
#include <thrift/server/TNonblockingServer.h>
@@ -64,13 +62,12 @@
#define AF_LOCAL AF_UNIX
#endif
-#if !defined(PRIu32)
-#define PRIu32 "I32u"
-#define PRIu64 "I64u"
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
#endif
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- #define AI_ADDRCONFIG 0x0400
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
#endif
namespace apache {
diff --git a/lib/cpp/src/thrift/transport/THeaderTransport.cpp b/lib/cpp/src/thrift/transport/THeaderTransport.cpp
index b1fe923..f222910 100644
--- a/lib/cpp/src/thrift/transport/THeaderTransport.cpp
+++ b/lib/cpp/src/thrift/transport/THeaderTransport.cpp
@@ -23,11 +23,11 @@
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TCompactProtocol.h>
+#include <limits>
#include <utility>
-#include <cassert>
#include <string>
-#include <zlib.h>
#include <string.h>
+#include <zlib.h>
using std::map;
using boost::shared_ptr;
@@ -172,7 +172,7 @@
* Reads a string from ptr, taking care not to reach headerBoundary
* Advances ptr on success
*
- * @param str output string
+ * @param str output string
* @throws CORRUPTED_DATA if size of string exceeds boundary
*/
void THeaderTransport::readString(uint8_t*& ptr,
@@ -198,7 +198,10 @@
uint8_t* ptr = reinterpret_cast<uint8_t*>(rBuf_.get() + 10);
// Catch integer overflow, check for reasonable header size
- assert(headerSize < 16384);
+ if (headerSize >= 16384) {
+ throw TTransportException(TTransportException::CORRUPTED_DATA,
+ "Header size is unreasonable");
+ }
headerSize *= 4;
const uint8_t* const headerBoundary = ptr + headerSize;
if (headerSize > sz) {
@@ -252,7 +255,7 @@
}
// Untransform the data section. rBuf will contain result.
- untransform(data, sz - (data - rBuf_.get())); // ignore header in size calc
+ untransform(data, safe_numeric_cast<uint32_t>(static_cast<ptrdiff_t>(sz) - (data - rBuf_.get())));
}
void THeaderTransport::untransform(uint8_t* ptr, uint32_t sz) {
@@ -375,7 +378,7 @@
}
uint32_t THeaderTransport::getWriteBytes() {
- return wBase_ - wBuf_.get();
+ return safe_numeric_cast<uint32_t>(wBase_ - wBuf_.get());
}
/**
@@ -384,7 +387,7 @@
* Automatically advances ptr to after the written portion
*/
void THeaderTransport::writeString(uint8_t*& ptr, const string& str) {
- uint32_t strLen = str.length();
+ int32_t strLen = safe_numeric_cast<int32_t>(str.length());
ptr += writeVarint32(strLen, ptr);
memcpy(ptr, str.c_str(), strLen); // no need to write \0
ptr += strLen;
@@ -394,7 +397,7 @@
writeHeaders_[key] = value;
}
-size_t THeaderTransport::getMaxWriteHeadersSize() const {
+uint32_t THeaderTransport::getMaxWriteHeadersSize() const {
size_t maxWriteHeadersSize = 0;
THeaderTransport::StringToStringMap::const_iterator it;
for (it = writeHeaders_.begin(); it != writeHeaders_.end(); ++it) {
@@ -402,7 +405,7 @@
// 2 varints32 + the strings themselves
maxWriteHeadersSize += 5 + 5 + (it->first).length() + (it->second).length();
}
- return maxWriteHeadersSize;
+ return safe_numeric_cast<uint32_t>(maxWriteHeadersSize);
}
void THeaderTransport::clearHeaders() {
@@ -431,7 +434,7 @@
if (clientType == THRIFT_HEADER_CLIENT_TYPE) {
// header size will need to be updated at the end because of varints.
// Make it big enough here for max varint size, plus 4 for padding.
- int headerSize = (2 + getNumTransforms()) * THRIFT_MAX_VARINT32_BYTES + 4;
+ uint32_t headerSize = (2 + getNumTransforms()) * THRIFT_MAX_VARINT32_BYTES + 4;
// add approximate size of info headers
headerSize += getMaxWriteHeadersSize();
@@ -479,11 +482,11 @@
// write info headers
// for now only write kv-headers
- uint16_t headerCount = writeHeaders_.size();
+ int32_t headerCount = safe_numeric_cast<int32_t>(writeHeaders_.size());
if (headerCount > 0) {
pkt += writeVarint32(infoIdType::KEYVALUE, pkt);
// Write key-value headers count
- pkt += writeVarint32(headerCount, pkt);
+ pkt += writeVarint32(static_cast<int32_t>(headerCount), pkt);
// Write info headers
map<string, string>::const_iterator it;
for (it = writeHeaders_.begin(); it != writeHeaders_.end(); ++it) {
@@ -494,7 +497,7 @@
}
// Fixups after varint size calculations
- headerSize = (pkt - headerStart);
+ headerSize = safe_numeric_cast<uint32_t>(pkt - headerStart);
uint8_t padding = 4 - (headerSize % 4);
headerSize += padding;
@@ -504,8 +507,13 @@
}
// Pkt size
+ ptrdiff_t szHbp = (headerStart - pktStart - 4);
+ if (static_cast<uint64_t>(szHbp) > static_cast<uint64_t>(std::numeric_limits<uint32_t>().max()) - (headerSize + haveBytes)) {
+ throw TTransportException(TTransportException::CORRUPTED_DATA,
+ "Header section size is unreasonable");
+ }
szHbo = headerSize + haveBytes // thrift header + payload
- + (headerStart - pktStart - 4); // common header section
+ + static_cast<uint32_t>(szHbp); // common header section
headerSizeN = htons(headerSize / 4);
memcpy(headerSizePtr, &headerSizeN, sizeof(headerSizeN));
diff --git a/lib/cpp/src/thrift/transport/THeaderTransport.h b/lib/cpp/src/thrift/transport/THeaderTransport.h
index bf82674..af20fe3 100644
--- a/lib/cpp/src/thrift/transport/THeaderTransport.h
+++ b/lib/cpp/src/thrift/transport/THeaderTransport.h
@@ -21,11 +21,18 @@
#define THRIFT_TRANSPORT_THEADERTRANSPORT_H_ 1
#include <bitset>
+#include <limits>
#include <vector>
#include <stdexcept>
#include <string>
#include <map>
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#elif HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
#include <boost/scoped_array.hpp>
#include <boost/shared_ptr.hpp>
@@ -135,8 +142,7 @@
void transform(uint8_t* ptr, uint32_t sz);
uint16_t getNumTransforms() const {
- int trans = writeTrans_.size();
- return trans;
+ return safe_numeric_cast<uint16_t>(writeTrans_.size());
}
void setTransform(uint16_t transId) { writeTrans_.push_back(transId); }
@@ -204,7 +210,7 @@
/**
* Returns the maximum number of bytes that write k/v headers can take
*/
- size_t getMaxWriteHeadersSize() const;
+ uint32_t getMaxWriteHeadersSize() const;
struct infoIdType {
enum idType {
diff --git a/lib/cpp/src/thrift/transport/THttpClient.cpp b/lib/cpp/src/thrift/transport/THttpClient.cpp
index c610636..732c7e4 100644
--- a/lib/cpp/src/thrift/transport/THttpClient.cpp
+++ b/lib/cpp/src/thrift/transport/THttpClient.cpp
@@ -22,6 +22,7 @@
#include <sstream>
#include <boost/algorithm/string.hpp>
+#include <thrift/config.h>
#include <thrift/transport/THttpClient.h>
#include <thrift/transport/TSocket.h>
@@ -102,7 +103,7 @@
std::ostringstream h;
h << "POST " << path_ << " HTTP/1.1" << CRLF << "Host: " << host_ << CRLF
<< "Content-Type: application/x-thrift" << CRLF << "Content-Length: " << len << CRLF
- << "Accept: application/x-thrift" << CRLF << "User-Agent: Thrift/" << VERSION
+ << "Accept: application/x-thrift" << CRLF << "User-Agent: Thrift/" << PACKAGE_VERSION
<< " (C++/THttpClient)" << CRLF << CRLF;
string header = h.str();
diff --git a/lib/cpp/src/thrift/transport/THttpServer.cpp b/lib/cpp/src/thrift/transport/THttpServer.cpp
index c89ab94..3bf3053 100644
--- a/lib/cpp/src/thrift/transport/THttpServer.cpp
+++ b/lib/cpp/src/thrift/transport/THttpServer.cpp
@@ -21,6 +21,7 @@
#include <sstream>
#include <iostream>
+#include <thrift/config.h>
#include <thrift/transport/THttpServer.h>
#include <thrift/transport/TSocket.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
@@ -124,7 +125,7 @@
// Construct the HTTP header
std::ostringstream h;
h << "HTTP/1.1 200 OK" << CRLF << "Date: " << getTimeRFC1123() << CRLF << "Server: Thrift/"
- << VERSION << CRLF << "Access-Control-Allow-Origin: *" << CRLF
+ << PACKAGE_VERSION << CRLF << "Access-Control-Allow-Origin: *" << CRLF
<< "Content-Type: application/x-thrift" << CRLF << "Content-Length: " << len << CRLF
<< "Connection: Keep-Alive" << CRLF << CRLF;
string header = h.str();
diff --git a/lib/cpp/src/thrift/transport/TServerSocket.cpp b/lib/cpp/src/thrift/transport/TServerSocket.cpp
index 87b6383..8b65319 100644
--- a/lib/cpp/src/thrift/transport/TServerSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TServerSocket.cpp
@@ -60,10 +60,6 @@
#endif // _WIN32
#endif
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- #define AI_ADDRCONFIG 0x0400
-#endif
-
template <class T>
inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) {
return reinterpret_cast<const SOCKOPT_CAST_T*>(v);
diff --git a/lib/cpp/src/thrift/transport/TSocket.cpp b/lib/cpp/src/thrift/transport/TSocket.cpp
index e1c106a..9fad590 100644
--- a/lib/cpp/src/thrift/transport/TSocket.cpp
+++ b/lib/cpp/src/thrift/transport/TSocket.cpp
@@ -53,10 +53,6 @@
#endif // _WIN32
#endif
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- #define AI_ADDRCONFIG 0x0400
-#endif
-
template <class T>
inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) {
return reinterpret_cast<const SOCKOPT_CAST_T*>(v);
diff --git a/lib/cpp/src/thrift/transport/TTransportException.h b/lib/cpp/src/thrift/transport/TTransportException.h
index 83e876a..dbbb971 100644
--- a/lib/cpp/src/thrift/transport/TTransportException.h
+++ b/lib/cpp/src/thrift/transport/TTransportException.h
@@ -20,6 +20,7 @@
#ifndef _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_
#define _THRIFT_TRANSPORT_TTRANSPORTEXCEPTION_H_ 1
+#include <boost/numeric/conversion/cast.hpp>
#include <string>
#include <thrift/Thrift.h>
@@ -83,6 +84,21 @@
/** Error code */
TTransportExceptionType type_;
};
+
+/**
+ * Legacy code in transport implementations have overflow issues
+ * that need to be enforced.
+ */
+template <typename To, typename From> To safe_numeric_cast(From i) {
+ try {
+ return boost::numeric_cast<To>(i);
+ }
+ catch (const std::bad_cast& bc) {
+ throw TTransportException(TTransportException::CORRUPTED_DATA,
+ bc.what());
+ }
+}
+
}
}
} // apache::thrift::transport
diff --git a/lib/cpp/src/thrift/windows/config.h b/lib/cpp/src/thrift/windows/config.h
index 674c260..cde9de6 100644
--- a/lib/cpp/src/thrift/windows/config.h
+++ b/lib/cpp/src/thrift/windows/config.h
@@ -36,6 +36,9 @@
#define USE_BOOST_THREAD 1
#endif
+// Something that defines PRId64 is required to build
+#define HAVE_INTTYPES_H 1
+
// VS2010 or later has stdint.h
#if _MSC_VER >= 1600
#define HAVE_STDINT_H 1
@@ -65,7 +68,6 @@
#pragma warning(disable : 4996) // Deprecated posix name.
-#define VERSION "1.0.0-dev"
#define HAVE_GETTIMEOFDAY 1
#define HAVE_SYS_STAT_H 1
diff --git a/lib/cpp/test/AllProtocolTests.tcc b/lib/cpp/test/AllProtocolTests.tcc
index 8c8eaa8..3e0b833 100644
--- a/lib/cpp/test/AllProtocolTests.tcc
+++ b/lib/cpp/test/AllProtocolTests.tcc
@@ -190,14 +190,14 @@
testNaked<TProto, int64_t>(0);
for (int64_t i = 0; i < 62; i++) {
- testNaked<TProto, int64_t>(1L << i);
- testNaked<TProto, int64_t>(-(1L << i));
+ testNaked<TProto, int64_t>(1LL << i);
+ testNaked<TProto, int64_t>(-(1LL << i));
}
testField<TProto, T_I64, int64_t>(0);
for (int i = 0; i < 62; i++) {
- testField<TProto, T_I64, int64_t>(1L << i);
- testField<TProto, T_I64, int64_t>(-(1L << i));
+ testField<TProto, T_I64, int64_t>(1LL << i);
+ testField<TProto, T_I64, int64_t>(-(1LL << i));
}
testNaked<TProto, double>(123.456);
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index cbeff08..74b0a09 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -19,7 +19,13 @@
include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
-#Make sure gen-cpp files can be included
+add_definitions("-D__STDC_LIMIT_MACROS")
+
+if (WITH_DYN_LINK_TEST)
+ add_definitions( -DBOOST_TEST_DYN_LINK )
+endif()
+
+# Make sure gen-cpp files can be included
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
# Create the thrift C++ test library
@@ -54,7 +60,6 @@
)
add_library(testgencpp_cob STATIC ${testgencpp_cob_SOURCES})
-
add_executable(Benchmark Benchmark.cpp)
target_link_libraries(Benchmark testgencpp)
LINK_AGAINST_THRIFT_LIBRARY(Benchmark thrift)
diff --git a/lib/cpp/test/JSONProtoTest.cpp b/lib/cpp/test/JSONProtoTest.cpp
index 301a064..2da3044 100644
--- a/lib/cpp/test/JSONProtoTest.cpp
+++ b/lib/cpp/test/JSONProtoTest.cpp
@@ -262,8 +262,9 @@
":[\"i8\",3,1,2,3]},\"13\":{\"lst\":[\"i16\",3,1,2,3]},\"14\":{\"lst\":[\"i64"
"\",3,1,2,3]}}";
+ const std::size_t bufSiz = strlen(json_string) * sizeof(char);
boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer(
- (uint8_t*)(json_string), strlen(json_string)*sizeof(char)));
+ (uint8_t*)(json_string), static_cast<uint32_t>(bufSiz)));
boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
OneOfEach ooe2;
diff --git a/lib/cpp/test/ZlibTest.cpp b/lib/cpp/test/ZlibTest.cpp
index e155970..a4387a9 100644
--- a/lib/cpp/test/ZlibTest.cpp
+++ b/lib/cpp/test/ZlibTest.cpp
@@ -17,13 +17,19 @@
* under the License.
*/
-#define __STDC_FORMAT_MACROS
-
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // needed for getopt_long
#endif
+#if (_MSC_VER <= 1700)
+// polynomial and std::fill_t warning happens in MSVC 2010, 2013, maybe others
+// https://svn.boost.org/trac/boost/ticket/11426
+#pragma warning(disable:4996)
+#endif
+
+#ifdef HAVE_STDINT_H
#include <stdint.h>
+#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
diff --git a/lib/cpp/test/processor/ServerThread.cpp b/lib/cpp/test/processor/ServerThread.cpp
index e9d468f..009c4c6 100644
--- a/lib/cpp/test/processor/ServerThread.cpp
+++ b/lib/cpp/test/processor/ServerThread.cpp
@@ -35,6 +35,8 @@
assert(!running_);
running_ = true;
+ helper_.reset(new Helper(this));
+
// Start the other thread
concurrency::PlatformThreadFactory threadFactory;
threadFactory.setDetached(false);
diff --git a/lib/cpp/test/processor/ServerThread.h b/lib/cpp/test/processor/ServerThread.h
index eed3469..3803e7e 100644
--- a/lib/cpp/test/processor/ServerThread.h
+++ b/lib/cpp/test/processor/ServerThread.h
@@ -71,8 +71,7 @@
class ServerThread {
public:
ServerThread(const boost::shared_ptr<ServerState>& state, bool autoStart)
- : helper_(new Helper(this)),
- port_(0),
+ : port_(0),
running_(false),
serving_(false),
error_(false),
diff --git a/test/cpp/CMakeLists.txt b/test/cpp/CMakeLists.txt
index 09850a8..1facfa4 100755
--- a/test/cpp/CMakeLists.txt
+++ b/test/cpp/CMakeLists.txt
@@ -33,7 +33,6 @@
include_directories("${CMAKE_CURRENT_BINARY_DIR}/gen-cpp")
include_directories("${PROJECT_SOURCE_DIR}/lib/cpp/src")
-
set(crosstestgencpp_SOURCES
gen-cpp/ThriftTest.cpp
gen-cpp/ThriftTest_types.cpp
diff --git a/test/cpp/Makefile.am b/test/cpp/Makefile.am
index 9c4ac07..d825bdd 100755
--- a/test/cpp/Makefile.am
+++ b/test/cpp/Makefile.am
@@ -107,7 +107,7 @@
$(THRIFT) --gen cpp $<
AM_CPPFLAGS = $(BOOST_CPPFLAGS) $(LIBEVENT_CPPFLAGS) -I$(top_srcdir)/lib/cpp/src -Igen-cpp
-AM_CXXFLAGS = -Wall -Wextra -pedantic
+AM_CXXFLAGS = -Wall -Wextra -pedantic -D__STDC_LIMIT_MACROS
AM_LDFLAGS = $(BOOST_LDFLAGS) $(LIBEVENT_LDFLAGS) $(ZLIB_LIBS)
clean-local:
diff --git a/test/cpp/src/TestClient.cpp b/test/cpp/src/TestClient.cpp
index c16d045..da20b89 100644
--- a/test/cpp/src/TestClient.cpp
+++ b/test/cpp/src/TestClient.cpp
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -17,8 +17,6 @@
* under the License.
*/
-#define __STDC_FORMAT_MACROS
-#include <inttypes.h>
#include <limits>
#include <locale>
#include <ios>
@@ -35,6 +33,13 @@
#include <thrift/async/TEvhttpClientChannel.h>
#include <thrift/server/TNonblockingServer.h> // <event.h>
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
#include <boost/shared_ptr.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
@@ -355,6 +360,10 @@
}
try {
+#ifdef _MSC_VER
+#pragma warning( push )
+#pragma warning( disable : 4566 )
+#endif
string str(
"}{Afrikaans, Alemannisch, Aragonés, العربية, مصرى, "
"Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, "
@@ -381,6 +390,9 @@
"Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, "
"Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, "
"Bân-lâm-gú, 粵語");
+#ifdef _MSC_VER
+#pragma warning( pop )
+#endif
cout << "testString(" << str << ") = " << flush;
testClient.testString(s, str);
cout << s << endl;
@@ -457,10 +469,10 @@
BASETYPE_IDENTITY_TEST(testI64, (int64_t)-1);
BASETYPE_IDENTITY_TEST(testI64, (int64_t)7000000000000000123LL);
BASETYPE_IDENTITY_TEST(testI64, (int64_t)-7000000000000000123LL);
- BASETYPE_IDENTITY_TEST(testI64, (int64_t)pow(2LL, 32));
- BASETYPE_IDENTITY_TEST(testI64, (int64_t)-pow(2LL, 32));
- BASETYPE_IDENTITY_TEST(testI64, (int64_t)pow(2LL, 32) + 1);
- BASETYPE_IDENTITY_TEST(testI64, (int64_t)-pow(2LL, 32) - 1);
+ BASETYPE_IDENTITY_TEST(testI64, (int64_t)pow(static_cast<double>(2LL), 32));
+ BASETYPE_IDENTITY_TEST(testI64, (int64_t)-pow(static_cast<double>(2LL), 32));
+ BASETYPE_IDENTITY_TEST(testI64, (int64_t)pow(static_cast<double>(2LL), 32) + 1);
+ BASETYPE_IDENTITY_TEST(testI64, (int64_t)-pow(static_cast<double>(2LL), 32) - 1);
BASETYPE_IDENTITY_TEST(testI64, numeric_limits<int64_t>::max());
BASETYPE_IDENTITY_TEST(testI64, numeric_limits<int64_t>::min());
@@ -472,19 +484,19 @@
BASETYPE_IDENTITY_TEST(testDouble, -1.0);
BASETYPE_IDENTITY_TEST(testDouble, -5.2098523);
BASETYPE_IDENTITY_TEST(testDouble, -0.000341012439638598279);
- BASETYPE_IDENTITY_TEST(testDouble, pow(2, 32));
- BASETYPE_IDENTITY_TEST(testDouble, pow(2, 32) + 1);
- BASETYPE_IDENTITY_TEST(testDouble, pow(2, 53) - 1);
- BASETYPE_IDENTITY_TEST(testDouble, -pow(2, 32));
- BASETYPE_IDENTITY_TEST(testDouble, -pow(2, 32) - 1);
- BASETYPE_IDENTITY_TEST(testDouble, -pow(2, 53) + 1);
+ BASETYPE_IDENTITY_TEST(testDouble, pow(static_cast<double>(2), 32));
+ BASETYPE_IDENTITY_TEST(testDouble, pow(static_cast<double>(2), 32) + 1);
+ BASETYPE_IDENTITY_TEST(testDouble, pow(static_cast<double>(2), 53) - 1);
+ BASETYPE_IDENTITY_TEST(testDouble, -pow(static_cast<double>(2), 32));
+ BASETYPE_IDENTITY_TEST(testDouble, -pow(static_cast<double>(2), 32) - 1);
+ BASETYPE_IDENTITY_TEST(testDouble, -pow(static_cast<double>(2), 53) + 1);
try {
- double expected = pow(10, 307);
+ double expected = pow(static_cast<double>(10), 307);
cout << "testDouble(" << expected << ") = " << flush;
double actual = testClient.testDouble(expected);
cout << "(" << actual << ")" << endl;
- if (expected - actual > pow(10, 292)) {
+ if (expected - actual > pow(static_cast<double>(10), 292)) {
cout << "*** FAILED ***" << endl
<< "Expected: " << expected << " but got: " << actual << endl;
}
@@ -496,11 +508,11 @@
}
try {
- double expected = pow(10, -292);
+ double expected = pow(static_cast<double>(10), -292);
cout << "testDouble(" << expected << ") = " << flush;
double actual = testClient.testDouble(expected);
cout << "(" << actual << ")" << endl;
- if (expected - actual > pow(10, -307)) {
+ if (expected - actual > pow(static_cast<double>(10), -307)) {
cout << "*** FAILED ***" << endl
<< "Expected: " << expected << " but got: " << actual << endl;
}
diff --git a/test/cpp/src/TestServer.cpp b/test/cpp/src/TestServer.cpp
index b437860..b86b34c 100644
--- a/test/cpp/src/TestServer.cpp
+++ b/test/cpp/src/TestServer.cpp
@@ -17,9 +17,6 @@
* under the License.
*/
-#define __STDC_FORMAT_MACROS
-#include <inttypes.h>
-
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PlatformThreadFactory.h>
#include <thrift/protocol/TBinaryProtocol.h>
@@ -41,6 +38,13 @@
#include <thrift/transport/TTransportUtils.h>
#include "ThriftTest.h"
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
#include <iostream>
#include <stdexcept>
#include <sstream>
diff --git a/tutorial/cpp/CMakeLists.txt b/tutorial/cpp/CMakeLists.txt
index 1feec2c..8634b41 100644
--- a/tutorial/cpp/CMakeLists.txt
+++ b/tutorial/cpp/CMakeLists.txt
@@ -44,9 +44,13 @@
add_executable(TutorialServer CppServer.cpp)
target_link_libraries(TutorialServer tutorialgencpp)
LINK_AGAINST_THRIFT_LIBRARY(TutorialServer thrift)
-target_link_libraries(TutorialServer ${ZLIB_LIBRARIES})
+if (ZLIB_FOUND)
+ target_link_libraries(TutorialServer ${ZLIB_LIBRARIES})
+endif ()
add_executable(TutorialClient CppClient.cpp)
target_link_libraries(TutorialClient tutorialgencpp)
LINK_AGAINST_THRIFT_LIBRARY(TutorialClient thrift)
-target_link_libraries(TutorialClient ${ZLIB_LIBRARIES})
+if (ZLIB_FOUND)
+ target_link_libraries(TutorialClient ${ZLIB_LIBRARIES})
+endif ()