Skip to content

Commit f73c7f1

Browse files
authored
Merge pull request #2270 from hzeller/feature-20241002-no-stream-read
If non-mmap: use stdio reading of files instead of std::stream
2 parents 3415c94 + 13e0cd0 commit f73c7f1

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

.github/workflows/verible-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ jobs:
462462
uses: actions/cache@v3
463463
with:
464464
path: "c:/users/runneradmin/_bazel_runneradmin"
465-
key: bazelcache_windows2_${{ steps.cache_timestamp.outputs.time }}
466-
restore-keys: bazelcache_windows2_
465+
key: bazelcache_windows_${{ steps.cache_timestamp.outputs.time }}
466+
restore-keys: bazelcache_windows_
467467

468468
- name: Install dependencies
469469
run: |

common/util/file_util.cc

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
#include "common/util/file_util.h"
1616

17+
#include <fcntl.h>
18+
1719
#include <algorithm>
1820
#include <cerrno>
21+
#include <cstdint>
22+
#include <cstdio>
1923
#include <cstdlib>
2024
#include <cstring>
2125
#include <filesystem>
22-
#include <fstream>
23-
#include <iostream>
2426
#include <memory>
2527
#include <string>
2628
#include <system_error>
@@ -34,10 +36,11 @@
3436
#include "common/util/logging.h"
3537

3638
#ifndef _WIN32
37-
#include <fcntl.h>
3839
#include <sys/mman.h>
3940
#include <sys/stat.h>
4041
#include <unistd.h>
42+
#else
43+
#include <io.h>
4144
#endif
4245

4346
namespace fs = std::filesystem;
@@ -154,34 +157,35 @@ absl::Status FileExists(const std::string &filename) {
154157

155158
absl::StatusOr<std::string> GetContentAsString(absl::string_view filename) {
156159
std::string content;
157-
std::ifstream fs;
158-
std::istream *stream = nullptr;
160+
FILE *stream = nullptr;
159161
const bool use_stdin = IsStdin(filename);
160162
if (use_stdin) {
161-
stream = &std::cin;
163+
#ifdef _WIN32
164+
_setmode(_fileno(stdin), _O_BINARY); // Work around DOS/Win silliness.
165+
#endif
166+
stream = stdin;
162167
} else {
163168
const std::string filename_str = std::string{filename};
164169
if (absl::Status status = FileExists(filename_str); !status.ok()) {
165170
return status; // Bail
166171
}
167-
fs.open(filename_str.c_str());
172+
stream = fopen(filename_str.c_str(), "rb");
168173
std::error_code err;
169174
const size_t prealloc = fs::file_size(filename_str, err);
170175
if (err.value() == 0) content.reserve(prealloc);
171-
stream = &fs;
172176
}
173-
if (!stream->good()) {
177+
if (!stream) {
174178
return CreateErrorStatusFromErrno(filename, "can't read");
175179
}
176180
char buffer[4096];
177-
while (stream->good() && !stream->eof()) {
178-
stream->read(buffer, sizeof(buffer));
179-
content.append(buffer, stream->gcount());
180-
}
181-
182-
// Allow stdin to be reopened for more input.
183-
if (use_stdin && std::cin.eof()) std::cin.clear();
184-
return std::move(content);
181+
int bytes_read;
182+
do {
183+
bytes_read = fread(buffer, 1, sizeof(buffer), stream);
184+
content.append(buffer, bytes_read);
185+
} while (bytes_read > 0);
186+
fclose(stream);
187+
188+
return content;
185189
}
186190

187191
static absl::StatusOr<std::unique_ptr<MemBlock>> AttemptMemMapFile(
@@ -244,11 +248,19 @@ absl::StatusOr<std::unique_ptr<MemBlock>> GetContentAsMemBlock(
244248
absl::Status SetContents(absl::string_view filename,
245249
absl::string_view content) {
246250
VLOG(1) << __FUNCTION__ << ": Writing file: " << filename;
247-
std::ofstream f(std::string(filename).c_str());
248-
if (!f.good()) return CreateErrorStatusFromErrno(filename, "can't write.");
249-
f << content;
250-
f.close();
251-
if (!f.good()) return CreateErrorStatusFromErrno(filename, "closing.");
251+
FILE *out = fopen(std::string(filename).c_str(), "wb");
252+
if (!out) return CreateErrorStatusFromErrno(filename, "can't write.");
253+
const int64_t expected_write = content.size();
254+
int64_t total_written = 0;
255+
while (!content.empty()) {
256+
int64_t w = fwrite(content.data(), 1, content.size(), out);
257+
total_written += w;
258+
content.remove_prefix(w);
259+
}
260+
const bool written_completely = (total_written == expected_write);
261+
if (fclose(out) != 0 || !written_completely) {
262+
return CreateErrorStatusFromErrno(filename, "closing.");
263+
}
252264
return absl::OkStatus();
253265
}
254266

verilog/analysis/checkers/token_stream_lint_rule.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ void TokenStreamLintRule::HandleSymbol(const verible::Symbol &symbol,
7575
return p->Tag().tag == TK_StringLiteral;
7676
});
7777
const auto &string_literal = SymbolCastToLeaf(**literal);
78-
if (absl::StrContains(string_literal.get().text(), "\\\n")) {
78+
if (absl::StrContains(string_literal.get().text(), "\\\n") ||
79+
absl::StrContains(string_literal.get().text(), "\\\r")) {
7980
violations_.insert(LintViolation(string_literal, kMessage, context));
8081
}
8182
}

verilog/analysis/checkers/token_stream_lint_rule_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ TEST(StringLiteralConcatenationTest, FunctionFailures) {
5656
"\"Humpty Dumpty sat on a wall. \\\nHumpty Dumpty had a great fall.\""},
5757
";",
5858
"\nendmodule"},
59+
{"module m;\n",
60+
"string tmp=",
61+
{kToken,
62+
"\"Humpty Dumpty discovers CRLF \\\r\nHumpty Dumpty CRinged.\""},
63+
";",
64+
"\nendmodule"},
5965
{"module m;\n",
6066
"string tmp=",
6167
{kToken,

verilog/tools/obfuscator/verilog_obfuscate.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include <sstream> // IWYU pragma: keep // for ostringstream
2626
#include <string> // for string, allocator, etc
2727

28+
#ifdef _WIN32
29+
#include <fcntl.h>
30+
#include <io.h>
31+
#endif
32+
2833
#include "absl/flags/flag.h"
2934
#include "absl/status/status.h"
3035
#include "absl/status/statusor.h"
@@ -70,6 +75,12 @@ static constexpr absl::string_view kBuiltinFunctions[] = {
7075
};
7176

7277
int main(int argc, char **argv) {
78+
#ifdef _WIN32
79+
// stdio: Windows messes with newlines by default. Fix this here.
80+
_setmode(_fileno(stdin), _O_BINARY);
81+
_setmode(_fileno(stdout), _O_BINARY);
82+
#endif
83+
7384
const auto usage = absl::StrCat("usage: ", argv[0],
7485
" [options] < original > output\n"
7586
R"(

0 commit comments

Comments
 (0)