• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 19153812334-2653

07 Nov 2025 12:09AM UTC coverage: 69.033% (+0.003%) from 69.03%
19153812334-2653

push

github

tstack
[tidy] add format-string warnings

54 of 97 new or added lines in 39 files covered. (55.67%)

3 existing lines in 2 files now uncovered.

50702 of 73446 relevant lines covered (69.03%)

435988.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.08
/src/sqlitepp.client.hh
1
/**
2
 * Copyright (c) 2022, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#ifndef lnav_sqlitepp_client_hh
31
#define lnav_sqlitepp_client_hh
32

33
#include <sqlite3.h>
34

35
#include "base/auto_mem.hh"
36
#include "base/intern_string.hh"
37
#include "base/lnav_log.hh"
38
#include "base/short_alloc.h"
39
#include "sql_util.hh"
40
#include "vtab_module.hh"
41

42
inline int
43
bind_to_sqlite(sqlite3_stmt* stmt, int index, const struct timeval& tv)
162✔
44
{
45
    char timestamp[64];
46

47
    sql_strftime(timestamp, sizeof(timestamp), tv, 'T');
162✔
48

49
    return sqlite3_bind_text(stmt, index, timestamp, -1, SQLITE_TRANSIENT);
324✔
50
}
51

52
inline int
53
bind_to_sqlite(sqlite3_stmt* stmt,
1✔
54
               int index,
55
               const std::chrono::system_clock::time_point& tp)
56
{
57
    auto epoch_ts = tp.time_since_epoch().count();
1✔
58

59
    return sqlite3_bind_int64(stmt, index, epoch_ts);
1✔
60
}
61

62
inline int
63
bind_to_sqlite(sqlite3_stmt* stmt, int index, const char* str)
64
{
65
    return sqlite3_bind_text(stmt, index, str, -1, SQLITE_TRANSIENT);
66
}
67

68
inline int
69
bind_to_sqlite(sqlite3_stmt* stmt, int index, intern_string_t ist)
112✔
70
{
71
    return sqlite3_bind_text(
112✔
72
        stmt, index, ist.get(), ist.size(), SQLITE_TRANSIENT);
224✔
73
}
74

75
inline int
76
bind_to_sqlite(sqlite3_stmt* stmt, int index, string_fragment sf)
2✔
77
{
78
    return sqlite3_bind_text(
2✔
79
        stmt, index, sf.data(), sf.length(), SQLITE_TRANSIENT);
2✔
80
}
81

82
inline int
83
bind_to_sqlite(sqlite3_stmt* stmt, int index, const std::string& str)
1,261✔
84
{
85
    return sqlite3_bind_text(
1,261✔
86
        stmt, index, str.c_str(), str.size(), SQLITE_TRANSIENT);
2,522✔
87
}
88

89
inline int
90
bind_to_sqlite(sqlite3_stmt* stmt, int index, int64_t i)
118✔
91
{
92
    return sqlite3_bind_int64(stmt, index, i);
118✔
93
}
94

95
template<typename... Args, std::size_t... Idx>
96
int
97
bind_values_helper(sqlite3_stmt* stmt,
1,285✔
98
                   std::index_sequence<Idx...> idxs,
99
                   Args... args)
100
{
101
    int rcs[] = {bind_to_sqlite(stmt, Idx + 1, args)...};
1,285✔
102

103
    for (size_t lpc = 0; lpc < idxs.size(); lpc++) {
2,941✔
104
        if (rcs[lpc] != SQLITE_OK) {
1,656✔
NEW
105
            log_error("Failed to bind column %zu in statement: %s -- %s",
×
106
                      lpc,
107
                      sqlite3_sql(stmt),
108
                      sqlite3_errstr(rcs[lpc]));
109
            return rcs[lpc];
×
110
        }
111
    }
112

113
    return SQLITE_OK;
1,285✔
114
}
115

116
template<typename... Args>
117
int
118
bind_values(sqlite3_stmt* stmt, Args... args)
1,285✔
119
{
120
    return bind_values_helper(
2,545✔
121
        stmt, std::make_index_sequence<sizeof...(Args)>(), args...);
2,545✔
122
}
123

124
struct prepared_stmt {
125
    explicit prepared_stmt(auto_mem<sqlite3_stmt> stmt)
2,722✔
126
        : ps_stmt(std::move(stmt))
2,722✔
127
    {
128
    }
2,722✔
129

130
    Result<void, std::string> execute()
1,932✔
131
    {
132
        auto rc = sqlite3_reset(this->ps_stmt.in());
1,932✔
133
        if (rc != SQLITE_OK) {
1,932✔
134
            return Err(std::string(
×
135
                sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in()))));
×
136
        }
137

138
        rc = sqlite3_step(this->ps_stmt.in());
1,932✔
139
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
1,932✔
140
            return Ok();
1,932✔
141
        }
142

143
        auto msg = std::string(
144
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())));
×
145
        return Err(msg);
×
146
    }
147

148
    struct end_of_rows {};
149
    struct fetch_error {
150
        std::string fe_msg;
151
    };
152

153
    void reset() { sqlite3_reset(this->ps_stmt.in()); }
10✔
154

155
    template<typename T>
156
    using fetch_result = mapbox::util::variant<T, end_of_rows, fetch_error>;
157

158
    template<typename T>
159
    fetch_result<T> fetch_row()
805✔
160
    {
161
        auto rc = sqlite3_step(this->ps_stmt.in());
805✔
162
        if (rc == SQLITE_OK || rc == SQLITE_DONE) {
805✔
163
            return end_of_rows{};
791✔
164
        }
165

166
        if (rc == SQLITE_ROW) {
14✔
167
            const auto argc = sqlite3_column_count(this->ps_stmt.in());
14✔
168
            stack_vector<sqlite3_value*>::allocator_type::arena_type arena;
14✔
169
            stack_vector<sqlite3_value*> argv(arena);
14✔
170
            argv.resize(argc);
14✔
171

172
            for (int lpc = 0; lpc < argc; lpc++) {
65✔
173
                argv[lpc] = sqlite3_column_value(this->ps_stmt.in(), lpc);
51✔
174
            }
175

176
            return from_sqlite<T>()(argc, argv.data(), 0);
14✔
177
        }
14✔
178

179
        return fetch_error{
180
            sqlite3_errmsg(sqlite3_db_handle(this->ps_stmt.in())),
181
        };
182
    }
183

184
    template<typename T, typename F>
185
    Result<void, fetch_error> for_each_row(F func)
767✔
186
    {
187
        std::optional<fetch_error> err;
767✔
188
        auto done = false;
767✔
189

190
        while (!done) {
1,543✔
191
            done = this->template fetch_row<T>().match(
1,552✔
192
                func,
193
                [](end_of_rows) { return true; },
767✔
194
                [&err](const fetch_error& fe) {
776✔
195
                    err = fe;
×
196
                    return true;
×
197
                });
198
        }
199

200
        if (err) {
767✔
201
            return Err(err.value());
×
202
        }
203

204
        return Ok();
767✔
205
    }
767✔
206

207
    auto_mem<sqlite3_stmt> ps_stmt;
208
};
209

210
template<typename... Args>
211
static Result<prepared_stmt, std::string>
212
prepare_stmt(sqlite3* db, const char* sql, Args... args)
2,724✔
213
{
214
    auto_mem<sqlite3_stmt> retval(sqlite3_finalize);
2,724✔
215

216
    if (sqlite3_prepare_v2(db, sql, -1, retval.out(), nullptr) != SQLITE_OK) {
2,724✔
217
        return Err(
218
            fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
4✔
219
                        sqlite3_errmsg(db)));
4✔
220
    }
221

222
    if (sizeof...(args) > 0) {
223
        if (bind_values(retval.in(), args...) != SQLITE_OK) {
1,173✔
224
            return Err(
225
                fmt::format(FMT_STRING("unable to prepare SQL statement: {}"),
×
226
                            sqlite3_errmsg(db)));
×
227
        }
228
    }
229

230
    return Ok(prepared_stmt{
5,444✔
231
        std::move(retval),
2,722✔
232
    });
5,444✔
233
}
2,724✔
234

235
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc