Skip to content

Commit 415d91c

Browse files
committed
Replace magic numbers with named constants
Replaced hardcoded values 64 and 1000 with descriptive constants: - BUFFER_SAFETY_MARGIN (64): Extra space for string operations - MAX_PAREN_SEARCH_DISTANCE (1000): Prevent runaway parsing Improves code readability and maintainability.
1 parent d8dfed5 commit 415d91c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

db/db_sqlite3.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#if HAVE_SQLITE3
1010
#include <sqlite3.h>
1111

12+
/* Constants for SQL processing limits */
13+
#define BUFFER_SAFETY_MARGIN 64 /* Extra space for string operations */
14+
#define MAX_PAREN_SEARCH_DISTANCE 1000 /* Prevent runaway parsing */
15+
1216
struct db_sqlite3 {
1317
/* The actual db connection. */
1418
sqlite3 *conn;
@@ -284,7 +288,7 @@ static char *normalize_varchar_to_text(const tal_t *ctx, const char *query)
284288

285289
/* INT(3) -> INTEGER(7) worst case: +4 bytes per conversion */
286290
size_t max_expansions = (query_len / 3) * 4;
287-
size_t buffer_size = query_len + max_expansions + 64;
291+
size_t buffer_size = query_len + max_expansions + BUFFER_SAFETY_MARGIN;
288292

289293
if (buffer_size < query_len)
290294
return NULL;
@@ -310,7 +314,7 @@ static char *normalize_varchar_to_text(const tal_t *ctx, const char *query)
310314
while (*src && *src != ')') {
311315
src++;
312316
/* Prevent runaway on malformed SQL */
313-
if (src - paren_start > 1000)
317+
if (src - paren_start > MAX_PAREN_SEARCH_DISTANCE)
314318
return NULL;
315319
}
316320
if (*src == ')') src++;

0 commit comments

Comments
 (0)