Skip to content

Commit 29dabbf

Browse files
committed
cleanups
1 parent 87ab525 commit 29dabbf

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

handlersocket/database.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,7 @@ dbcontext::modify_record(dbcallback_i& cb, TABLE *const table,
588588
continue;
589589
}
590590
const long long pval = fld->val_int();
591-
char buf[nv.size() + 1];
592-
memcpy(buf, nv.begin(), nv.size());
593-
buf[nv.size()] = 0;
594-
const long long llv = atoll(buf);
591+
const long long llv = atoll_nocheck(nv.begin(), nv.end());
595592
long long nval = 0;
596593
if (mod_op == '+') {
597594
/* increment */

libhsclient/string_util.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,46 @@ atoi_tmpl_nocheck(const char *start, const char *finish)
4141
return v;
4242
}
4343

44+
template <typename T> T
45+
atoi_signed_tmpl_nocheck(const char *start, const char *finish)
46+
{
47+
T v = 0;
48+
bool negative = false;
49+
if (start != finish) {
50+
if (start[0] == '-') {
51+
++start;
52+
negative = true;
53+
} else if (start[0] == '+') {
54+
++start;
55+
}
56+
}
57+
for (; start != finish; ++start) {
58+
const char c = *start;
59+
if (c < '0' || c > '9') {
60+
break;
61+
}
62+
v *= 10;
63+
if (negative) {
64+
v -= static_cast<T>(c - '0');
65+
} else {
66+
v += static_cast<T>(c - '0');
67+
}
68+
}
69+
return v;
70+
}
71+
4472
uint32_t
4573
atoi_uint32_nocheck(const char *start, const char *finish)
4674
{
4775
return atoi_tmpl_nocheck<uint32_t>(start, finish);
4876
}
4977

78+
long long
79+
atoll_nocheck(const char *start, const char *finish)
80+
{
81+
return atoi_signed_tmpl_nocheck<long long>(start, finish);
82+
}
83+
5084
void
5185
append_uint32(string_buffer& buf, uint32_t v)
5286
{

libhsclient/string_util.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ string_wref get_token(char *& wp, char *wp_end, char delim);
3434
uint32_t atoi_uint32_nocheck(const char *start, const char *finish);
3535
std::string to_stdstring(uint32_t v);
3636
void append_uint32(string_buffer& buf, uint32_t v);
37+
long long atoll_nocheck(const char *start, const char *finish);
3738

3839
int errno_string(const char *s, int en, std::string& err_r);
3940

regtest/test_01_lib/test11.expected

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ VAL
2424
[0][k7][-84]
2525
[0][k8][0]
2626
INCREMENT
27-
[0][1]
28-
[0][1]
29-
[0][1]
30-
[0][1]
31-
[0][1]
32-
[0][1]
33-
[0][1]
34-
[0][1]
35-
[0][1]
27+
[0][6]
28+
[0][7]
29+
[0][8]
30+
[0][9]
31+
[0][10]
32+
[0][11]
33+
[0][12]
34+
[0][13]
35+
[0][14]
3636
VAL
3737
[0][k5][15]

0 commit comments

Comments
 (0)