Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception instead of exit when Secp256k1::getByte() is not a hex #258

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions bsgsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ email: albertobsd@gmail.com
#include <time.h>
#include <vector>
#include <inttypes.h>
#include <stdexcept>
#include "base58/libbase58.h"
#include "rmd160/rmd160.h"
#include "oldbloom/oldbloom.h"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions secp256k1/SECP256K1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <cstdio>
#include <cstring>
#include <stdexcept>
#include "SECP256k1.h"
#include "Point.h"
#include "../util.h"
Expand Down Expand Up @@ -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;
}
Expand Down