Skip to content

Commit 06fcca8

Browse files
committed
tool: do not segfault if keyfile doesn't exist, or not enough arguments
It's generally a good idea to check whether argc[2] is not null *before* using it.
1 parent 0d364cd commit 06fcca8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

tool/main.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,36 @@ static int tool_sign(int argc, const char *argv[])
9898
{
9999
libecdsaauth_key_t *key;
100100
char *signature = NULL;
101-
char *in = strdup(argv[2]);
101+
char *in = NULL;
102102
size_t siglen;
103103

104-
if (argv[1] == NULL)
104+
if (argc < 3)
105105
{
106106
fprintf(stderr, "usage: ecdsatool sign privatekey.pem base64challenge\n");
107107
return EXIT_FAILURE;
108108
}
109109

110110
key = libecdsaauth_key_load(argv[1]);
111+
if (key == NULL)
112+
{
113+
fprintf(stderr, "loading key failed\n");
114+
return EXIT_FAILURE;
115+
}
116+
in = strdup(argv[2]);
117+
111118
libecdsaauth_sign_base64(key, in, strlen(in) + 1, &signature, &siglen);
119+
120+
free(in);
112121
libecdsaauth_key_free(key);
113122

114-
if (signature != NULL)
123+
if (signature == NULL)
115124
{
116-
printf("%s\n", signature);
117-
free(signature);
118-
}
119-
else
120125
fprintf(stderr, "signing failed\n");
126+
return EXIT_FAILURE;
127+
}
121128

122-
free(in);
129+
printf("%s\n", signature);
130+
free(signature);
123131

124132
return EXIT_SUCCESS;
125133
}

0 commit comments

Comments
 (0)