Skip to content

Commit c03339d

Browse files
Merge pull request Expensify#712 from Expensify/chirag-moving-fn-libstuff
Moving method to libstuff so thats its accessible in server-e
2 parents dc5e5d5 + 12145dc commit c03339d

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

libstuff/libstuff.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,3 +2544,27 @@ void STerminateHandler(void) {
25442544
// And we're out.
25452545
abort();
25462546
}
2547+
2548+
bool SIsValidSQLiteDateModifier(const string& modifier) {
2549+
// See: https://www.sqlite.org/lang_datefunc.html
2550+
list<string> parts = SParseList(SToUpper(modifier));
2551+
for (const string& part : parts) {
2552+
// Simple regexp validation
2553+
if (SREMatch("^(\\+|-)\\d{1,3} (YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)S?$", part)) {
2554+
continue;
2555+
}
2556+
if (SREMatch("^START OF (DAY|MONTH|YEAR)$", part)) {
2557+
continue;
2558+
}
2559+
if (SREMatch("^WEEKDAY [0-6]$", part)) {
2560+
continue;
2561+
}
2562+
2563+
// Couldn't match this part to any valid syntax
2564+
SINFO("Syntax error, failed parsing date modifier '" << modifier << "' on part '" << part << "'");
2565+
return false;
2566+
}
2567+
2568+
// Matched all parts, valid syntax
2569+
return true;
2570+
}

libstuff/libstuff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ inline bool SContains(const string& haystack, char needle) { return haystack.fin
486486
inline bool SContains(const STable& nameValueMap, const string& name) {
487487
return (nameValueMap.find(name) != nameValueMap.end());
488488
}
489+
bool SIsValidSQLiteDateModifier(const string& modifier);
489490

490491
// General testing functions
491492
inline bool SIEquals(const string& lhs, const string& rhs) { return !strcasecmp(lhs.c_str(), rhs.c_str()); }

plugins/Jobs.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ bool BedrockPlugin_Jobs::peekCommand(SQLite& db, BedrockCommand& command) {
246246
}
247247

248248
// Validate retryAfter
249-
if (SContains(job, "retryAfter") && job["retryAfter"] != "" && !_isValidSQLiteDateModifier(job["retryAfter"])){
249+
if (SContains(job, "retryAfter") && job["retryAfter"] != "" && !SIsValidSQLiteDateModifier(job["retryAfter"])){
250250
STHROW("402 Malformed retryAfter");
251251
}
252252

@@ -1285,7 +1285,7 @@ string BedrockPlugin_Jobs::_constructNextRunDATETIME(const string& lastScheduled
12851285

12861286
for (const string& part : parts) {
12871287
// Validate the sqlite date modifiers
1288-
if (!_isValidSQLiteDateModifier(part)){
1288+
if (!SIsValidSQLiteDateModifier(part)){
12891289
SWARN("Syntax error, failed parsing repeat "+part);
12901290
return "";
12911291
}
@@ -1314,30 +1314,6 @@ bool BedrockPlugin_Jobs::_hasPendingChildJobs(SQLite& db, int64_t jobID) {
13141314
return !result.empty();
13151315
}
13161316

1317-
bool BedrockPlugin_Jobs::_isValidSQLiteDateModifier(const string& modifier) {
1318-
// See: https://www.sqlite.org/lang_datefunc.html
1319-
list<string> parts = SParseList(SToUpper(modifier));
1320-
for (const string& part : parts) {
1321-
// Simple regexp validation
1322-
if (SREMatch("^(\\+|-)\\d{1,3} (YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)S?$", part)) {
1323-
continue;
1324-
}
1325-
if (SREMatch("^START OF (DAY|MONTH|YEAR)$", part)) {
1326-
continue;
1327-
}
1328-
if (SREMatch("^WEEKDAY [0-6]$", part)) {
1329-
continue;
1330-
}
1331-
1332-
// Couldn't match this part to any valid syntax
1333-
SINFO("Syntax error, failed parsing date modifier '" << modifier << "' on part '" << part << "'");
1334-
return false;
1335-
}
1336-
1337-
// Matched all parts, valid syntax
1338-
return true;
1339-
}
1340-
13411317
void BedrockPlugin_Jobs::_validatePriority(const int64_t priority) {
13421318
// We'd initially intended for any value to be allowable here, but for
13431319
// performance reasons, we currently will only allow specific values to

plugins/Jobs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ class BedrockPlugin_Jobs : public BedrockPlugin {
2828
bool _validateRepeat(const string& repeat) { return !_constructNextRunDATETIME("", "", repeat).empty(); }
2929
void _validatePriority(const int64_t priority);
3030
bool _hasPendingChildJobs(SQLite& db, int64_t jobID);
31-
bool _isValidSQLiteDateModifier(const string& modifier);
3231
};

0 commit comments

Comments
 (0)