This commit is contained in:
Nikita Taranov 2024-12-11 16:46:02 +01:00
parent fe55995e04
commit 0b3f7d18ef
3 changed files with 26 additions and 26 deletions

View File

@ -9,9 +9,9 @@
static void readAndAssert(DB::ReadBuffer & buf, const char * str) static void readAndAssert(DB::ReadBuffer & buf, const char * str)
{ {
size_t n = strlen(str); size_t n = strlen(str);
char tmp[n]; std::vector<char> tmp(n);
buf.readStrict(tmp, n); buf.readStrict(tmp.data(), n);
ASSERT_EQ(strncmp(tmp, str, n), 0); ASSERT_EQ(strncmp(tmp.data(), str, n), 0);
} }
static void assertAvailable(DB::ReadBuffer & buf, const char * str) static void assertAvailable(DB::ReadBuffer & buf, const char * str)

View File

@ -252,7 +252,7 @@ int compressFiles(const char* out_name, const char* exec, char* filenames[], int
/// Store information about each file and compress it /// Store information about each file and compress it
FileData* files_data = new FileData[count + is_exec]; FileData* files_data = new FileData[count + is_exec];
const char * names[count + is_exec]; std::vector<const char *> names(count + is_exec);
for (int i = 0; i <= count; ++i) for (int i = 0; i <= count; ++i)
{ {
const char* filename = nullptr; const char* filename = nullptr;
@ -342,7 +342,7 @@ int compressFiles(const char* out_name, const char* exec, char* filenames[], int
/// save location of files information /// save location of files information
metadata.start_of_files_data = htole64(pointer); metadata.start_of_files_data = htole64(pointer);
if (0 != saveMetaData(names, count + is_exec, output_fd, metadata, files_data, pointer, sum_file_size)) if (0 != saveMetaData(names.data(), count + is_exec, output_fd, metadata, files_data, pointer, sum_file_size))
{ {
delete [] files_data; delete [] files_data;
return 1; return 1;

View File

@ -240,32 +240,32 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress
size_t file_path_len = path ? strlen(path) + 1 + file_name_len : file_name_len; size_t file_path_len = path ? strlen(path) + 1 + file_name_len : file_name_len;
char file_name[file_path_len]; std::vector<char> file_name(file_path_len);
memset(file_name, '\0', file_path_len); memset(file_name.data(), '\0', file_path_len);
if (path) if (path)
{ {
strcat(file_name, path); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcat(file_name.data(), path); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
strcat(file_name, "/"); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcat(file_name.data(), "/"); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
} }
bool same_name = false; bool same_name = false;
if (file_info.exec) if (file_info.exec)
{ {
has_exec = true; has_exec = true;
strcat(file_name, name); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcat(file_name.data(), name); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
} }
else else
{ {
if (strcmp(name, input + files_pointer) == 0) if (strcmp(name, input + files_pointer) == 0)
same_name = true; same_name = true;
strcat(file_name, input + files_pointer); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcat(file_name.data(), input + files_pointer); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
} }
files_pointer += le64toh(file_info.name_length); files_pointer += le64toh(file_info.name_length);
if (file_info.exec || same_name) if (file_info.exec || same_name)
{ {
strcat(file_name, ".decompressed.XXXXXX"); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcat(file_name.data(), ".decompressed.XXXXXX"); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
int fd = mkstemp(file_name); int fd = mkstemp(file_name.data());
if (fd == -1) if (fd == -1)
{ {
perror("mkstemp"); perror("mkstemp");
@ -273,12 +273,12 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress
} }
if (0 != close(fd)) if (0 != close(fd))
perror("close"); perror("close");
strncpy(decompressed_suffix, file_name + strlen(file_name) - 6, 6); strncpy(decompressed_suffix, file_name.data() + strlen(file_name.data()) - 6, 6);
*decompressed_umask = le64toh(file_info.umask); *decompressed_umask = le64toh(file_info.umask);
have_compressed_analoge = true; have_compressed_analoge = true;
} }
int output_fd = open(file_name, O_RDWR | O_CREAT, le64toh(file_info.umask)); int output_fd = open(file_name.data(), O_RDWR | O_CREAT, le64toh(file_info.umask));
if (output_fd == -1) if (output_fd == -1)
{ {
@ -330,7 +330,7 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress
perror("close"); perror("close");
if (is_sudo) if (is_sudo)
chown(file_name, info_in.st_uid, info_in.st_gid); chown(file_name.data(), info_in.st_uid, info_in.st_gid);
} }
if (0 != munmap(input, info_in.st_size)) if (0 != munmap(input, info_in.st_size))
@ -415,19 +415,19 @@ int main(int/* argc*/, char* argv[])
return 1; return 1;
} }
char file_path[strlen(self) + 1]; std::vector<char> file_path(strlen(self) + 1);
strcpy(file_path, self); // NOLINT(clang-analyzer-security.insecureAPI.strcpy) strcpy(file_path.data(), self); // NOLINT(clang-analyzer-security.insecureAPI.strcpy)
char * path = nullptr; char * path = nullptr;
char * name = strrchr(file_path, '/'); char * name = strrchr(file_path.data(), '/');
if (name) if (name)
{ {
path = file_path; path = file_path.data();
*name = 0; *name = 0;
++name; ++name;
} }
else else
name = file_path; name = file_path.data();
struct stat input_info; struct stat input_info;
if (0 != stat(self, &input_info)) if (0 != stat(self, &input_info))
@ -526,20 +526,20 @@ int main(int/* argc*/, char* argv[])
{ {
const char * const decompressed_name_fmt = "%s.decompressed.%s"; const char * const decompressed_name_fmt = "%s.decompressed.%s";
int decompressed_name_len = snprintf(nullptr, 0, decompressed_name_fmt, self, decompressed_suffix); int decompressed_name_len = snprintf(nullptr, 0, decompressed_name_fmt, self, decompressed_suffix);
char decompressed_name[decompressed_name_len + 1]; std::vector<char> decompressed_name(decompressed_name_len + 1);
(void)snprintf(decompressed_name, decompressed_name_len + 1, decompressed_name_fmt, self, decompressed_suffix); (void)snprintf(decompressed_name.data(), decompressed_name_len + 1, decompressed_name_fmt, self, decompressed_suffix);
#if defined(OS_DARWIN) #if defined(OS_DARWIN)
// We can't just rename it on Mac due to security issues, so we copy it... // We can't just rename it on Mac due to security issues, so we copy it...
std::error_code ec; std::error_code ec;
std::filesystem::copy_file(static_cast<char *>(decompressed_name), static_cast<char *>(self), ec); std::filesystem::copy_file(static_cast<char *>(decompressed_name.data()), static_cast<char *>(self), ec);
if (ec) if (ec)
{ {
std::cerr << ec.message() << std::endl; std::cerr << ec.message() << std::endl;
return 1; return 1;
} }
#else #else
if (link(decompressed_name, self)) if (link(decompressed_name.data(), self))
{ {
perror("link"); perror("link");
return 1; return 1;
@ -551,7 +551,7 @@ int main(int/* argc*/, char* argv[])
return 1; return 1;
} }
if (unlink(decompressed_name)) if (unlink(decompressed_name.data()))
{ {
perror("unlink"); perror("unlink");
return 1; return 1;