From 94a9fb3580f63605c1bcc8a1510b7c738fec26da Mon Sep 17 00:00:00 2001
From: Jonas Leder <jonas@jonasled.de>
Date: Tue, 22 Mar 2022 13:20:22 +0100
Subject: [PATCH] fix abusing exceptions to check if row exists in db
---
app/redirectShortenURL.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/app/redirectShortenURL.py b/app/redirectShortenURL.py
index a12d2bf..1306984 100644
--- a/app/redirectShortenURL.py
+++ b/app/redirectShortenURL.py
@@ -21,11 +21,11 @@ def redirect_shorten_url(request, short_url):
abort(500)
if not error_404: #If there was no 404 error before, redirect the user. If not throw a 404 error
res = cursor.execute('SELECT CALLS FROM ANALYTICS WHERE SHORT_URL=?', [host + "/" + short_url.lower()])
- try:
- calls = res.fetchone()[0] + 1
+ row = row = cursor.fetchone()
+ if not row is None:
+ calls = row[0] + 1
cursor.execute('UPDATE ANALYTICS SET CALLS = ? WHERE SHORT_URL=?', (str(calls), host + "/" + short_url.lower()))
- except:
- print(Exception)
+ else:
cursor.execute('INSERT INTO ANALYTICS (SHORT_URL) VALUES ("' + host + "/" + short_url.lower() + "\")")
return redirect(url) #I use temp redirect here, because the owner of a link can delete it. If then the link is reused, the user will maybe redirected to the wrong page
--
GitLab