Skip to content

Commit

Permalink
SNOW-983100: replace banned functions (#659)
Browse files Browse the repository at this point in the history
* replace banned functions

* deprecate sf_getenv, sf_strerror and sf_free_s
  • Loading branch information
Harry Xi authored Feb 15, 2024
1 parent 6c5312a commit 1e4eb43
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 366 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ endif ()
set(CMAKE_VERBOSE_MAKEFILE ON)
if (UNIX)
# Linux and OSX
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -std=gnu99 -g -fPIC -Werror -Wno-error=deprecated-declarations ${MOCK_OBJECT_WRAPPER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -std=gnu99 -g -fPIC -Werror -Wno-error=deprecated-declarations -D_LARGEFILE64_SOURCE ${MOCK_OBJECT_WRAPPER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -std=gnu++11 -fPIC -Werror -Wno-error=deprecated-declarations ${MOCK_OBJECT_WRAPPER_FLAGS}")
else()
# Windows
Expand Down
8 changes: 4 additions & 4 deletions cpp/FileTransferAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ RemoteStorageRequestOutcome Snowflake::Client::FileTransferAgent::downloadSingle
FileMetadata *fileMetadata,
size_t resultIndex)
{
char strerr_buf[SF_ERROR_BUFSIZE];

fileMetadata->destPath = std::string(response.localLocation) + PATH_SEP +
fileMetadata->destFileName;
std::string destPathPlatform = m_stmtPutGet->UTF8ToPlatformString(fileMetadata->destPath);
Expand All @@ -847,20 +849,18 @@ RemoteStorageRequestOutcome Snowflake::Client::FileTransferAgent::downloadSingle
}
catch (...) {
std::string err = "Could not open file " + fileMetadata->destPath + " to downoad";
char* str_error = sf_strerror(errno);
char* str_error = sf_strerror_s(errno, strerr_buf, sizeof(strerr_buf));
CXX_LOG_DEBUG("Could not open file %s to downoad: %s",
fileMetadata->destPath.c_str(), str_error);
sf_free_s(str_error);
m_executionResults->SetTransferOutCome(outcome, resultIndex);
break;
}
if (!dstFile.is_open())
{
std::string err = "Could not open file " + fileMetadata->destPath + " to downoad";
char* str_error = sf_strerror(errno);
char* str_error = sf_strerror_s(errno, strerr_buf, sizeof(strerr_buf));
CXX_LOG_DEBUG("Could not open file %s to downoad: %s",
fileMetadata->destPath.c_str(), str_error);
sf_free_s(str_error);
m_executionResults->SetTransferOutCome(outcome, resultIndex);
break;
}
Expand Down
5 changes: 2 additions & 3 deletions cpp/SnowflakeAzureClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ SnowflakeAzureClient::SnowflakeAzureClient(StageInfo *stageInfo,
CXX_LOG_TRACE("ca bundle file from SF_GLOBAL_CA_BUNDLE_FILE *%s*", caBundleFile);
}
if( caBundleFile[0] == 0 ) {
char* capath = sf_getenv("SNOWFLAKE_TEST_CA_BUNDLE_FILE");
char capath_buf[MAX_PATH + 2];
char* capath = sf_getenv_s("SNOWFLAKE_TEST_CA_BUNDLE_FILE", capath_buf, sizeof(capath_buf));
if (capath) {
if (strlen(capath) > MAX_PATH - 1) {
sf_free_s(capath);
throw SnowflakeTransferException(TransferError::INTERNAL_ERROR,
"CA bundle file path too long.");
}
if (!sb_strcpy(caBundleFile, (size_t)MAX_PATH, capath)) {
caBundleFile[0] = 0;
}
sf_free_s(capath);
CXX_LOG_TRACE("ca bundle file from SNOWFLAKE_TEST_CA_BUNDLE_FILE *%s*", caBundleFile);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/logger/SFLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Snowflake::Client::ISFLogger * Snowflake::Client::SFLogger::getExternalLogger()
void log_masked_va_list(FILE* fp, const char *fmt, va_list args)
{
std::string maskedMsg = Snowflake::Client::SFLogger::getMaskedMsgVA(fmt, args);
fprintf(fp, "%s", maskedMsg.c_str());
sb_fprintf(fp, "%s", maskedMsg.c_str());
}

std::string Snowflake::Client::SFLogger::getMaskedMsg(const char* fmt, ...)
Expand Down
17 changes: 8 additions & 9 deletions cpp/util/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include "snowflake/platform.h"

namespace {
char* get_env_or(const char* name) {
return sf_getenv(name);
char* get_env_or(char* outbuf, size_t bufsize, const char* name) {
return sf_getenv_s(name, outbuf, bufsize);
}
template <typename... Args>
char* get_env_or(const char* prime, Args... fallback)
char* get_env_or(char *outbuf, size_t bufsize, const char* prime, Args... fallback)
{
char* value = get_env_or(prime);
return value ? value : get_env_or(fallback...);
char* value = get_env_or(outbuf, bufsize, prime);
return value ? value : get_env_or(outbuf, bufsize, fallback...);
}
}

Expand Down Expand Up @@ -82,18 +82,17 @@ void Snowflake::Client::Util::Proxy::clearPwd() {
}

void Snowflake::Client::Util::Proxy::setProxyFromEnv() {
char* env_value = get_env_or("all_proxy", "https_proxy", "http_proxy");
char valbuf[1024];
char* env_value = get_env_or(valbuf, sizeof(valbuf), "all_proxy", "https_proxy", "http_proxy");
if (env_value != nullptr) {
std::string proxy(env_value);
sf_free_s(env_value);
stringToProxyParts(proxy);
}

// Get noproxy string
env_value = get_env_or("no_proxy", "NO_PROXY");
env_value = get_env_or(valbuf, sizeof(valbuf), "no_proxy", "NO_PROXY");
if (env_value != nullptr) {
m_noProxy = env_value;
sf_free_s(env_value);
}
}

Expand Down
3 changes: 2 additions & 1 deletion cpp/util/ThreadPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class ThreadPool {
int err = pthread_key_create(&key, NULL);
if (err)
{
CXX_LOG_ERROR("Thread pool creating key failed with error: %s", strerror(err));
char strerrbuf[SF_ERROR_BUFSIZE];
CXX_LOG_ERROR("Thread pool creating key failed with error: %s", sf_strerror_s(err, strerrbuf, sizeof(strerrbuf)));
throw SnowflakeTransferException(TransferError::INTERNAL_ERROR,
"Thread context fail to initialize");
}
Expand Down
Loading

0 comments on commit 1e4eb43

Please sign in to comment.