Skip to content

Commit

Permalink
replace MSFT banned functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jz committed Oct 26, 2023
1 parent 7cde388 commit 75ef7b5
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 41 deletions.
11 changes: 7 additions & 4 deletions cpp/FileTransferAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "EncryptionProvider.hpp"
#include "logger/SFLogger.hpp"
#include "snowflake/platform.h"
#include "snowflake/Simba_CRTFunctionSafe.h"
#include <chrono>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -616,12 +617,14 @@ void Snowflake::Client::FileTransferAgent::compressSourceFile(
stagingFile += m_stmtPutGet->UTF8ToPlatformString(fileMetadata->destFileName);
std::string srcFileNamePlatform = m_stmtPutGet->UTF8ToPlatformString(fileMetadata->srcFileName);

FILE *sourceFile = fopen(srcFileNamePlatform.c_str(), "r");
FILE *sourceFile;
sb_fopen(&sourceFile, fileMetadata->srcFileName.c_str(), "r");
if( !sourceFile ){
CXX_LOG_ERROR("Failed to open srcFileName %s. Errno: %d", fileMetadata->srcFileName.c_str(), errno);
throw SnowflakeTransferException(TransferError::FILE_OPEN_ERROR, srcFileNamePlatform.c_str(), -1);
}
FILE *destFile = fopen(stagingFile.c_str(), "w");
FILE *destFile;
sb_fopen(&destFile, stagingFile.c_str(), "w");
if ( !destFile) {
CXX_LOG_ERROR("Failed to open srcFileToUpload file %s. Errno: %d", stagingFile.c_str(), errno);
throw SnowflakeTransferException(TransferError::FILE_OPEN_ERROR, stagingFile.c_str(), -1);
Expand Down Expand Up @@ -847,15 +850,15 @@ RemoteStorageRequestOutcome Snowflake::Client::FileTransferAgent::downloadSingle
catch (...) {
std::string err = "Could not open file " + fileMetadata->destPath + " to downoad";
CXX_LOG_DEBUG("Could not open file %s to downoad: %s",
fileMetadata->destPath.c_str(), std::strerror(errno));
fileMetadata->destPath.c_str(), sf_strerror(errno));
m_executionResults->SetTransferOutCome(outcome, resultIndex);
break;
}
if (!dstFile.is_open())
{
std::string err = "Could not open file " + fileMetadata->destPath + " to downoad";
CXX_LOG_DEBUG("Could not open file %s to downoad: %s",
fileMetadata->destPath.c_str(), std::strerror(errno));
fileMetadata->destPath.c_str(), sf_strerror(errno));
m_executionResults->SetTransferOutCome(outcome, resultIndex);
break;
}
Expand Down
3 changes: 2 additions & 1 deletion cpp/SnowflakeAzureClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2019 Snowflake Computing, Inc. All rights reserved.
*/

#include "snowflake/Simba_CRTFunctionSafe.h"
#include "SnowflakeAzureClient.hpp"
#include "FileMetadataInitializer.hpp"
#include "snowflake/client.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ SnowflakeAzureClient::SnowflakeAzureClient(StageInfo *stageInfo,
CXX_LOG_TRACE("ca bundle file from SF_GLOBAL_CA_BUNDLE_FILE *%s*", caBundleFile);
}
if( caBundleFile[0] == 0 ) {
const char* capath = std::getenv("SNOWFLAKE_TEST_CA_BUNDLE_FILE");
const char* capath = sf_getenv("SNOWFLAKE_TEST_CA_BUNDLE_FILE");
if (capath) {
if (strlen(capath) > MAX_PATH - 1) {
throw SnowflakeTransferException(TransferError::INTERNAL_ERROR,
Expand Down
2 changes: 1 addition & 1 deletion cpp/crypto/CipherContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ size_t CipherContext::finalize(void *const out)
// EXTERNAL_DECRYPT_ERROR_MSG);
else
{
printf("Finialize failed, op %d", static_cast<int>(impl.op));
sb_printf("Finialize failed, op %d", static_cast<int>(impl.op));
throw;
}
//SF_THROW_CRYPTO(static_cast<int>(impl.op));
Expand Down
2 changes: 1 addition & 1 deletion cpp/lib/Authenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace Client
const std::string &passcode)
{
FILE *file;
file = fopen(privateKeyFile.c_str(), "r");
file = sb_fopen(&file, privateKeyFile.c_str(), "r");
if (file == nullptr)
{
CXX_LOG_ERROR("Failed to open private key file. Errno: %d", errno);
Expand Down
21 changes: 11 additions & 10 deletions cpp/util/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "snowflake/Proxy.hpp"
#include "snowflake/Simba_CRTFunctionSafe.h"

Snowflake::Client::Util::Proxy::Proxy(const std::string &proxy_str)
{
Expand Down Expand Up @@ -71,12 +72,12 @@ void Snowflake::Client::Util::Proxy::setProxyFromEnv() {
std::string proxy;

// Get proxy string and set scheme
if (std::getenv("all_proxy")) {
proxy = std::getenv("all_proxy");
} else if (std::getenv("https_proxy")) {
proxy = std::getenv("https_proxy");
} else if (std::getenv("http_proxy")) {
proxy = std::getenv("http_proxy");
if (sf_getenv("all_proxy")) {
proxy = sf_getenv("all_proxy");
} else if (sf_getenv("https_proxy")) {
proxy = sf_getenv("https_proxy");
} else if (sf_getenv("http_proxy")) {
proxy = sf_getenv("http_proxy");
}

if (!proxy.empty())
Expand All @@ -85,10 +86,10 @@ void Snowflake::Client::Util::Proxy::setProxyFromEnv() {
}

// Get noproxy string
if (std::getenv("no_proxy")) {
m_noProxy = std::getenv("no_proxy");
} else if (std::getenv("NO_PROXY")) {
m_noProxy = std::getenv("NO_PROXY");
if (sf_getenv("no_proxy")) {
m_noProxy = sf_getenv("no_proxy");
} else if (sf_getenv("NO_PROXY")) {
m_noProxy = sf_getenv("NO_PROXY");
}
}

Expand Down
84 changes: 84 additions & 0 deletions include/snowflake/Simba_CRTFunctionSafe.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@
#ifndef _SIMBA_CRTFUNCTIONSAFE_H_
#define _SIMBA_CRTFUNCTIONSAFE_H_

#define __STDC_WANT_LIB_EXT1__ 1

#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef STDCALL
// match snowflake define in platform.h
#ifdef _WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
#endif

extern char* STDCALL sf_getenv(const char *name);
extern char* STDCALL sf_strerror(int);

#if defined(WIN32) || defined(_WIN64) // For Windows

/// @brief Copy a string.
Expand Down Expand Up @@ -193,6 +211,42 @@
return ret;
}

/// @brief Write formatted string to a FILE*. (See the standard C fprintf for details).
///
/// @param in_stream Stream to write to. (NOT OWN)
/// @param in_format Format control string. (NOT OWN)
///
/// @return The number of bytes written to the stream, or a negative value if an error occurred.
static inline int sb_fprintf(FILE* in_stream, const char* in_format, ...)
{
va_list vlist;
va_start(vlist, in_format);
int bytesWritten = vfprintf_s(in_stream, in_format, vlist);
va_end(vlist);

return bytesWritten;
}


/// @brief Write formatted string to a stdout*. (See the standard C fprintf for details).
///
/// @param in_format Format control string. (NOT OWN)
///
/// @return The number of bytes written to the stream, or a negative value if an error occurred.
static inline int sb_printf(const char* in_format, ...)
{
va_list vlist;
va_start(vlist, in_format);
int bytesWritten = vfprintf_s(stdout, in_format, vlist);
va_end(vlist);

return bytesWritten;
}


#define sb_getenv sf_getenv
#define sb_strerror sf_strerror

#else // For all other platforms except Windows

/// @brief Copy a string.
Expand Down Expand Up @@ -331,7 +385,37 @@
}

#define sb_sscanf sscanf
#define sb_fprintf fprintf
#define sb_printf printf
#define sb_strerror strerror

#define sb_getenv getenv

#endif

/// @brief Open a file.
///
/// @param out_file A pointer to the file pointer that will receive the pointer to the
/// opened file. (NOT OWN)
/// @param in_filename The name of the file to open. (NOT OWN)
/// @param in_mode Type of access permitted. (NOT OWN)
///
/// @return A pointer to the opened file; a NULL pointer if an error occurred. (OWN)
static inline FILE* sb_fopen(FILE** out_file, const char* in_filename, const char* in_mode)
{
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
return fopen_s(out_file, in_filename, in_mode) ? NULL : *out_file;
#else
return *out_file = fopen(in_filename, in_mode);
#endif
}



#ifdef __cplusplus
}
#endif



#endif
1 change: 1 addition & 0 deletions include/snowflake/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ int STDCALL sf_unsetenv(const char *name);

int STDCALL sf_mkdir(const char *path);

char* STDCALL sf_strerror(int errnum);

int STDCALL
_thread_init(SF_THREAD_HANDLE *thread, void *(*proc)(void *), void *arg);
Expand Down
8 changes: 4 additions & 4 deletions lib/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,14 @@ static sf_bool STDCALL log_init(const char *log_path, SF_LOG_LEVEL log_level) {
if (LOG_PATH != NULL) {
// Create log file path (if it already doesn't exist)
if (mkpath(LOG_PATH) == -1) {
fprintf(stderr, "Error creating log directory. Error code: %s\n",
strerror(errno));
sb_fprintf(stderr, "Error creating log directory. Error code: %s\n",
sf_strerror(errno));
goto cleanup;
}
// Set the log path only, the log file will be created when actual log output is needed.
log_set_path(LOG_PATH);
} else {
fprintf(stderr,
sb_fprintf(stderr,
"Log path is NULL. Was there an error during path construction?\n");
goto cleanup;
}
Expand Down Expand Up @@ -539,7 +539,7 @@ SF_STATUS STDCALL snowflake_global_init(
sf_error_init();
if (!log_init(log_path, log_level)) {
// no way to log error because log_init failed.
fprintf(stderr, "Error during log initialization");
sb_fprintf(stderr, "Error during log initialization");
goto cleanup;
}
CURLcode curl_ret = curl_global_init(CURL_GLOBAL_DEFAULT);
Expand Down
10 changes: 5 additions & 5 deletions lib/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ void dump(const char *text,
/* without the hex output, we can fit more on screen */
width = 0x40;

fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
sb_fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
text, (long) size, (long) size);

for (i = 0; i < size; i += width) {

fprintf(stream, "%4.4lx: ", (long) i);
sb_fprintf(stream, "%4.4lx: ", (long) i);

if (!nohex) {
/* hex not disabled, show it */
for (c = 0; c < width; c++)
if (i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
sb_fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
Expand All @@ -62,7 +62,7 @@ void dump(const char *text,
i += (c + 2 - width);
break;
}
fprintf(stream, "%c",
sb_fprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c]
: '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
Expand All @@ -87,7 +87,7 @@ int my_trace(CURL *handle, curl_infotype type,

switch (type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
sb_fprintf(stderr, "== Info: %s", data);
/* FALLTHROUGH */
default: /* in case a new one is introduced to shock us */
return 0;
Expand Down
10 changes: 5 additions & 5 deletions lib/http_perform.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ void dump(const char *text,
/* without the hex output, we can fit more on screen */
width = 0x40;

fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
sb_fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
text, (long) size, (long) size);

for (i = 0; i < size; i += width) {

fprintf(stream, "%4.4lx: ", (long) i);
sb_fprintf(stream, "%4.4lx: ", (long) i);

if (!nohex) {
/* hex not disabled, show it */
for (c = 0; c < width; c++)
if (i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
sb_fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
Expand All @@ -74,7 +74,7 @@ void dump(const char *text,
i += (c + 2 - width);
break;
}
fprintf(stream, "%c",
sb_fprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c]
: '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
Expand All @@ -99,7 +99,7 @@ int my_trace(CURL *handle, curl_infotype type,

switch (type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
sb_fprintf(stderr, "== Info: %s", data);
/* FALLTHROUGH */
default: /* in case a new one is introduced to shock us */
return 0;
Expand Down
16 changes: 8 additions & 8 deletions lib/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ log_log_va_list(int level, const char *file, int line, const char *ns,
/* Log to stderr */
if (!L.quiet) {
#ifdef LOG_USE_COLOR
fprintf(
sb_fprintf(
stderr, SF_LOG_TIMESTAMP_FORMAT_COLOR,
tsbuf, level_colors[level], level_names[level], ns, basename,
line);
#else
fprintf(
sb_fprintf(
stderr, SF_LOG_TIMESTAMP_FORMAT,
buf, level_names[level], namespace, basename, line);
#endif
Expand All @@ -135,7 +135,7 @@ log_log_va_list(int level, const char *file, int line, const char *ns,
va_copy(copy, args);
log_masked_va_list(stderr, fmt, copy);
va_end(copy);
fprintf(stderr, "\n");
sb_fprintf(stderr, "\n");
fflush(stderr);
}

Expand All @@ -145,22 +145,22 @@ log_log_va_list(int level, const char *file, int line, const char *ns,
*/
if (!(L.fp) && L.path)
{
L.fp = fopen(L.path, "w+");
sb_fopen(&L.fp, L.path, "w+");
if (!(L.fp))
{
fprintf(stderr,
sb_fprintf(stderr,
"Error opening file from file path: %s\nError code: %s\n",
L.path, strerror(errno));
L.path, sf_strerror(errno));
L.path = NULL;
}
}

if (L.fp) {
fprintf(
sb_fprintf(
L.fp, SF_LOG_TIMESTAMP_FORMAT,
tsbuf, level_names[level], ns, basename, line);
log_masked_va_list(L.fp, fmt, args);
fprintf(L.fp, "\n");
sb_fprintf(L.fp, "\n");
fflush(L.fp);
}

Expand Down
Loading

0 comments on commit 75ef7b5

Please sign in to comment.