]> git.sesse.net Git - plocate/blobdiff - database-builder.cpp
Reset string explicitely to prevent undefined behavior
[plocate] / database-builder.cpp
index 985585838308fc7518a148c4d5473a14cdc79968..0655681ffe2a1ca3e61384ca0484257c6e253e31 100644 (file)
@@ -490,7 +490,7 @@ DatabaseBuilder::DatabaseBuilder(const char *outfile, gid_t owner, int block_siz
        int fd = -1;
 #ifdef O_TMPFILE
        fd = open(path.c_str(), O_WRONLY | O_TMPFILE, 0640);
        int fd = -1;
 #ifdef O_TMPFILE
        fd = open(path.c_str(), O_WRONLY | O_TMPFILE, 0640);
-       if (fd == -1 && errno != EOPNOTSUPP) {
+       if (fd == -1 && errno != EOPNOTSUPP && errno != EISDIR) {
                perror(path.c_str());
                exit(1);
        }
                perror(path.c_str());
                exit(1);
        }
@@ -531,7 +531,15 @@ DatabaseBuilder::DatabaseBuilder(const char *outfile, gid_t owner, int block_siz
        hdr.max_version = 2;
        hdr.filename_index_offset_bytes = -1;
        hdr.zstd_dictionary_length_bytes = -1;
        hdr.max_version = 2;
        hdr.filename_index_offset_bytes = -1;
        hdr.zstd_dictionary_length_bytes = -1;
+       hdr.zstd_dictionary_offset_bytes = -1; // Dictionary offset is not known yet.
        hdr.check_visibility = check_visibility;
        hdr.check_visibility = check_visibility;
+       hdr.directory_data_length_bytes = 0;
+       hdr.directory_data_offset_bytes = 0;
+       hdr.next_zstd_dictionary_length_bytes = 0;
+       hdr.next_zstd_dictionary_offset_bytes = 0;
+       hdr.conf_block_length_bytes = 0;
+       hdr.conf_block_offset_bytes = 0;
+
        fwrite(&hdr, sizeof(hdr), 1, outfp);
 
        if (dictionary.empty()) {
        fwrite(&hdr, sizeof(hdr), 1, outfp);
 
        if (dictionary.empty()) {
@@ -543,13 +551,6 @@ DatabaseBuilder::DatabaseBuilder(const char *outfile, gid_t owner, int block_siz
                hdr.zstd_dictionary_length_bytes = dictionary.size();
                cdict = ZSTD_createCDict(dictionary.data(), dictionary.size(), /*level=*/6);
        }
                hdr.zstd_dictionary_length_bytes = dictionary.size();
                cdict = ZSTD_createCDict(dictionary.data(), dictionary.size(), /*level=*/6);
        }
-
-       hdr.directory_data_length_bytes = 0;
-       hdr.directory_data_offset_bytes = 0;
-       hdr.next_zstd_dictionary_length_bytes = 0;
-       hdr.next_zstd_dictionary_offset_bytes = 0;
-       hdr.conf_block_length_bytes = 0;
-       hdr.conf_block_offset_bytes = 0;
 }
 
 DatabaseReceiver *DatabaseBuilder::start_corpus(bool store_dir_times)
 }
 
 DatabaseReceiver *DatabaseBuilder::start_corpus(bool store_dir_times)
@@ -683,6 +684,10 @@ void DatabaseBuilder::finish_corpus()
        fseek(outfp, 0, SEEK_SET);
        fwrite(&hdr, sizeof(hdr), 1, outfp);
 
        fseek(outfp, 0, SEEK_SET);
        fwrite(&hdr, sizeof(hdr), 1, outfp);
 
+       // This is needed on systems that simulate linkat() by copying
+       // the contents of the file instead of linking.
+       fflush(outfp);
+
        if (!temp_filename.empty()) {
                if (rename(temp_filename.c_str(), outfile.c_str()) == -1) {
                        perror("rename");
        if (!temp_filename.empty()) {
                if (rename(temp_filename.c_str(), outfile.c_str()) == -1) {
                        perror("rename");