Skip to content

Commit 325ce5e

Browse files
committed
[nits] more bug fixing
1 parent 142ef0d commit 325ce5e

29 files changed

Lines changed: 175 additions & 136 deletions

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ add_library(
700700
highlighter.hh
701701
hotkeys.hh
702702
input_dispatcher.hh
703-
itertools.similar.hh
704703
k_merge_tree.h
705704
lnav.events.hh
706705
lnav.exec-phase.hh

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ noinst_HEADERS = \
309309
hotkeys.hh \
310310
init.sql \
311311
input_dispatcher.hh \
312-
itertools.similar.hh \
313312
k_merge_tree.h \
314313
line_buffer.hh \
315314
listview_curses.hh \

src/base/auto_fd.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,23 @@ auto_fd::operator=(int fd)
165165
}
166166

167167
Result<void, std::string>
168-
auto_fd::write_fully(string_fragment sf)
168+
auto_fd::write_fully(const unsigned char* data, size_t len)
169169
{
170-
while (!sf.empty()) {
171-
auto rc = write(this->af_fd, sf.data(), sf.length());
170+
while (len > 0) {
171+
auto rc = write(this->af_fd, data, len);
172172

173173
if (rc < 0) {
174174
if (errno == EINTR) {
175175
continue;
176176
}
177177
return Err(
178178
fmt::format(FMT_STRING("failed to write {} bytes to FD {}"),
179-
sf.length(),
179+
len,
180180
this->af_fd));
181181
}
182182

183-
sf = sf.substr(rc);
183+
data += rc;
184+
len -= rc;
184185
}
185186

186187
return Ok();

src/base/auto_fd.hh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,27 @@ public:
163163
*/
164164
void reset(int fd = -1);
165165

166-
Result<void, std::string> write_fully(string_fragment sf);
166+
Result<void, std::string> write_fully(const unsigned char* data,
167+
size_t len);
168+
169+
Result<void, std::string> write_fully(string_fragment sf)
170+
{
171+
return this->write_fully(sf.udata(), sf.length());
172+
}
173+
174+
template<std::size_t N>
175+
Result<void, std::string> write_fully(const unsigned char (&str)[N])
176+
{
177+
return this->write_fully(str, N);
178+
}
179+
180+
template<typename T,
181+
std::enable_if_t<std::is_integral_v<T>, bool> = true>
182+
Result<void, std::string> write_fully(const T& value)
183+
{
184+
return this->write_fully(
185+
reinterpret_cast<const unsigned char*>(&value), sizeof(value));
186+
}
167187

168188
void close_on_exec() const;
169189

src/base/cell_container.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,13 @@ cell_container::cursor::get_text_length() const
409409
return sub;
410410
}
411411

412-
size_t retval = this->c_chunk->cc_data[this->c_offset + 1]
413-
| (this->c_chunk->cc_data[this->c_offset + 2] << 8)
414-
| (this->c_chunk->cc_data[this->c_offset + 3] << 16)
415-
| (this->c_chunk->cc_data[this->c_offset + 4] << 24);
412+
size_t retval
413+
= static_cast<size_t>(this->c_chunk->cc_data[this->c_offset + 1])
414+
| (static_cast<size_t>(this->c_chunk->cc_data[this->c_offset + 2]) << 8)
415+
| (static_cast<size_t>(this->c_chunk->cc_data[this->c_offset + 3])
416+
<< 16)
417+
| (static_cast<size_t>(this->c_chunk->cc_data[this->c_offset + 4])
418+
<< 24);
416419

417420
return retval;
418421
}

src/base/piper.file.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
namespace lnav::piper {
4141

42-
const char HEADER_MAGIC[4] = {'L', 0, 'N', 1};
42+
const unsigned char HEADER_MAGIC[4] = {'L', 0, 'N', 1};
4343

4444
const std::filesystem::path&
4545
storage_path()

src/base/piper.file.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct header {
7575
const std::filesystem::path& storage_path();
7676

7777
constexpr size_t HEADER_SIZE = 8;
78-
extern const char HEADER_MAGIC[4];
78+
extern const unsigned char HEADER_MAGIC[4];
7979

8080
std::optional<auto_buffer> read_header(int fd, const char* first8);
8181

src/base/time_util.cc

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <date/ptz.h>
3838

3939
#include "config.h"
40+
#include "itertools.similar.hh"
4041
#include "lnav_log.hh"
4142

4243
namespace lnav {
@@ -87,8 +88,10 @@ strftime_rfc3339(char* buffer,
8788
}
8889

8990
ssize_t
90-
strftime_rfc3339(
91-
char* buffer, size_t buffer_size, std::chrono::microseconds micros, char sep)
91+
strftime_rfc3339(char* buffer,
92+
size_t buffer_size,
93+
std::chrono::microseconds micros,
94+
char sep)
9295
{
9396
struct tm gmtm;
9497
auto secs = std::chrono::duration_cast<std::chrono::seconds>(micros);
@@ -228,6 +231,39 @@ local_time_to_info(date::local_seconds secs)
228231
return TZ_POSIX_UTC.value().get_info(secs);
229232
}
230233

234+
inline attr_line_t&
235+
symbol_reducer(const std::string& elem, attr_line_t& accum)
236+
{
237+
return accum.append("\n ").append(lnav::roles::symbol(elem));
238+
}
239+
240+
Result<const date::time_zone*, lnav::console::user_message>
241+
locate_zone(string_fragment tz_name)
242+
{
243+
try {
244+
return Ok(date::locate_zone(tz_name.to_string_view()));
245+
} catch (const std::runtime_error& e) {
246+
attr_line_t note;
247+
248+
try {
249+
note = (date::get_tzdb().zones
250+
| lnav::itertools::map(&date::time_zone::name)
251+
| lnav::itertools::similar_to(tz_name.to_string())
252+
| lnav::itertools::fold(symbol_reducer, attr_line_t{}))
253+
.add_header("did you mean one of the following?");
254+
} catch (const std::runtime_error& e) {
255+
log_error("unable to get timezones: %s", e.what());
256+
}
257+
auto um = lnav::console::user_message::error(
258+
attr_line_t().append_quoted(tz_name).append(
259+
" is not a valid timezone"))
260+
.with_reason(e.what())
261+
.with_note(note)
262+
.move();
263+
return Err(um);
264+
}
265+
}
266+
231267
} // namespace lnav
232268

233269
static time_t BAD_DATE = -1;
@@ -283,8 +319,7 @@ tm2sec(const struct tm* t)
283319
if (secs < 0) {
284320
return BAD_DATE;
285321
} /* must have overflowed */
286-
else
287-
{
322+
else {
288323
#ifdef HAVE_STRUCT_TM_TM_ZONE
289324
if (t->tm_zone) {
290325
secs -= t->tm_gmtoff;

src/base/time_util.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "config.h"
4444
#include "date/date.h"
4545
#include "date/tz.h"
46+
#include "lnav.console.hh"
47+
#include "result.h"
4648

4749
inline std::chrono::microseconds
4850
to_us(const timeval& tv)
@@ -91,6 +93,9 @@ struct duration_hasher {
9193
}
9294
};
9395

96+
Result<const date::time_zone*, lnav::console::user_message>
97+
locate_zone(string_fragment tz_name);
98+
9499
} // namespace lnav
95100

96101
tm* secs2tm(lnav::time64_t tim, tm* res);

0 commit comments

Comments
 (0)