aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-12-06 10:34:24 +0100
committerGitHub <noreply@github.com>2023-12-06 10:34:24 +0100
commit851a7bb5e57a4521feec338bf5b4cc7e099b927b (patch)
tree7db4affac59e5f82c7295c95a7935e3d1b71972c
parent0754f11f6bb14b209e7739c5c1af44f23bc67743 (diff)
parenta5ffef40b0124a88c3ad3b9f3a8c27922f54f11f (diff)
Merge pull request #29562 from vespa-engine/balder/avoid-null-format-warningv8.270.8
- Use std::string for pattern to avoid apparent gcc13 bug incorrectly…
-rw-r--r--fbench/src/splitfile/splitfile.cpp24
1 files changed, 12 insertions, 12 deletions
diff --git a/fbench/src/splitfile/splitfile.cpp b/fbench/src/splitfile/splitfile.cpp
index 8eddc163463..18005e9ce42 100644
--- a/fbench/src/splitfile/splitfile.cpp
+++ b/fbench/src/splitfile/splitfile.cpp
@@ -3,6 +3,7 @@
#include <fstream>
#include <vector>
#include <memory>
+#include <cassert>
#include <unistd.h>
/**
@@ -16,22 +17,21 @@ int
main(int argc, char** argv)
{
// parameters with default values.
- const char *pattern = "query%03d.txt";
+ std::string pattern = "query%03d.txt";
int linebufsize = 10240;
// parse options and override defaults.
int opt;
- bool optError;
+ bool optError = false;
- optError = false;
while((opt = getopt(argc, argv, "p:m:")) != -1) {
switch(opt) {
case 'p':
- pattern = optarg;
- if (pattern == nullptr) {
+ if (optarg == nullptr) {
printf("Missing 'pattern' argument to -p option !\n");
return -1;
}
+ pattern = optarg;
break;
case 'm':
linebufsize = atoi(optarg);
@@ -65,10 +65,6 @@ main(int argc, char** argv)
return -1;
}
- int i;
- int res;
- std::vector<char> linebuf(linebufsize);
- char filename[1024];
std::unique_ptr<FileReader> input = std::make_unique<FileReader>();
std::vector<std::unique_ptr<std::ostream>> output;
@@ -86,8 +82,10 @@ main(int argc, char** argv)
// open output files
output.reserve(outcnt);
- for (i = 0; i < outcnt; i++) {
- snprintf(filename, 1024, pattern, i);
+ for (int i = 0; i < outcnt; i++) {
+ char filename[1024];
+ int written = snprintf(filename, sizeof(filename), pattern.c_str(), i);
+ assert(written < int(sizeof(filename)));
output.emplace_back(std::make_unique<std::ofstream>(filename, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc));
if (! output.back()) {
printf("could not open output file: %s\n", filename);
@@ -97,11 +95,13 @@ main(int argc, char** argv)
}
// split file
+ std::vector<char> linebuf(linebufsize);
+ int res;
while ((res = input->ReadLine(&linebuf[0], linebufsize - 1)) >= 0) {
if (res < linebufsize - 1) {
linebuf[res] = '\n';
linebuf[res + 1] = '\0'; // just in case
- i = random() % outcnt;
+ int i = random() % outcnt;
if (!output[i]->write(&linebuf[0], res + 1)) {
printf("error writing to file '%d'\n", i);
}