From 8559fd3379da0c2942ff67c14de4ca6101ee6983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20M=C3=BAdry?= Date: Wed, 16 Feb 2022 17:01:18 +0100 Subject: [PATCH] new brackets crash fix version Fixes parentheses depth and wraps fastRows() in try/except --- src/forum.nim | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/forum.nim b/src/forum.nim index 264f21f..1babfde 100644 --- a/src/forum.nim +++ b/src/forum.nim @@ -1607,7 +1607,17 @@ routes: get "/search.json": cond "q" in request.params - let q = @"q" + var query = @"q" + + var depth = 0 + for ch in query: + if ch == '(': depth.inc + elif ch == ')': depth.dec + if depth == 0: discard + elif depth > 0: query.add ')'.repeat(depth) + elif depth < 0: query = '('.repeat(abs(depth)) & query + + let q = query cond q.len > 0 var results: seq[SearchResult] = @[] @@ -1618,20 +1628,23 @@ routes: q, q, $count, $0, q, q, $count, $0, q ] - for rowFT in fastRows(db, queryFT, data): - var content = rowFT[3] - try: content = content.rstToHtml() except EParseError: discard - results.add( - SearchResult( - kind: SearchResultKind(rowFT[^1].parseInt()), - threadId: rowFT[0].parseInt(), - threadTitle: rowFT[1], - postId: rowFT[2].parseInt(), - postContent: content, - creation: rowFT[4].parseInt(), - author: selectUser(rowFT[5 .. 11]), + try: + for rowFT in fastRows(db, queryFT, data): + var content = rowFT[3] + try: content = content.rstToHtml() except EParseError: discard + results.add( + SearchResult( + kind: SearchResultKind(rowFT[^1].parseInt()), + threadId: rowFT[0].parseInt(), + threadTitle: rowFT[1], + postId: rowFT[2].parseInt(), + postContent: content, + creation: rowFT[4].parseInt(), + author: selectUser(rowFT[5 .. 11]), + ) ) - ) + except DbError: + discard resp Http200, $(%results), "application/json"