From 7d20a1bcb37b15730ffe67e0aa5e4f71cc53e6bf Mon Sep 17 00:00:00 2001 From: Vladimir Tarkhanov Date: Tue, 12 Sep 2023 11:08:26 +0300 Subject: [PATCH 1/2] Throw an exception instead of exit when Secp256k1::getByte() is not a hexadecimal value --- bsgsd.cpp | 24 +++++++++++++++++------- secp256k1/SECP256K1.cpp | 4 ++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bsgsd.cpp b/bsgsd.cpp index cf33d6e..5bcc461 100644 --- a/bsgsd.cpp +++ b/bsgsd.cpp @@ -11,6 +11,7 @@ email: albertobsd@gmail.com #include #include #include +#include #include "base58/libbase58.h" #include "rmd160/rmd160.h" #include "oldbloom/oldbloom.h" @@ -2376,13 +2377,22 @@ void* client_handler(void* arg) { pthread_exit(NULL); } - if(!secp->ParsePublicKeyHex(t.tokens[0],OriginalPointsBSGS,OriginalPointsBSGScompressed)) { - printf("Invalid publickey format from client %s\n",t.tokens[0]); - freetokenizer(&t); - sendstr(client_fd,"400 Bad Request"); - close(client_fd); - pthread_exit(NULL); - } + try { + if(!secp->ParsePublicKeyHex(t.tokens[0],OriginalPointsBSGS,OriginalPointsBSGScompressed)) { + printf("Invalid publickey format from client %s\n",t.tokens[0]); + freetokenizer(&t); + sendstr(client_fd,"400 Bad Request"); + close(client_fd); + pthread_exit(NULL); + } + } catch (const std::invalid_argument &e) { + printf("%s\n", e.what()); + freetokenizer(&t); + sendstr(client_fd,"400 Bad Request"); + close(client_fd); + pthread_exit(NULL); + } + if(!(isValidHex(t.tokens[1]) && isValidHex(t.tokens[1]))) { printf("Invalid hexadecimal format from client %s:%s\n",t.tokens[1],t.tokens[2]); freetokenizer(&t); diff --git a/secp256k1/SECP256K1.cpp b/secp256k1/SECP256K1.cpp index 46f918d..727f931 100644 --- a/secp256k1/SECP256K1.cpp +++ b/secp256k1/SECP256K1.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "SECP256k1.h" #include "Point.h" #include "../util.h" @@ -94,8 +95,7 @@ uint8_t Secp256K1::GetByte(char *str, int idx) { tmp[1] = str[2 * idx + 1]; tmp[2] = 0; if (sscanf(tmp, "%X", &val) != 1) { - printf("ParsePublicKeyHex: Error invalid public key specified (unexpected hexadecimal digit)\n"); - exit(-1); + throw new std::invalid_argument("ParsePublicKeyHex: Error invalid public key specified (unexpected hexadecimal digit)"); } return (uint8_t)val; } From a32782c27a8cf42807a52e3ec7c4f3652c6081a3 Mon Sep 17 00:00:00 2001 From: Vladimir Tarkhanov Date: Tue, 12 Sep 2023 12:39:07 +0300 Subject: [PATCH 2/2] fix pointer --- bsgsd.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bsgsd.cpp b/bsgsd.cpp index 5bcc461..70d5dfe 100644 --- a/bsgsd.cpp +++ b/bsgsd.cpp @@ -2385,8 +2385,8 @@ void* client_handler(void* arg) { close(client_fd); pthread_exit(NULL); } - } catch (const std::invalid_argument &e) { - printf("%s\n", e.what()); + } catch (const std::invalid_argument *e) { + printf("%s\n", e->what()); freetokenizer(&t); sendstr(client_fd,"400 Bad Request"); close(client_fd);