Fix unsafe strtoul

This commit is contained in:
Marc
2022-10-04 14:18:12 +02:00
parent 3534b709bd
commit c223d149f4
+17 -5
View File
@@ -67,14 +67,15 @@ int usage() {
} }
int validateAppId(const char * appIdStr) { int validateAppId(const char * appIdStr) {
char * strtoulSentinel;
//strtoul set EINVAL(after C99) if the string is invalid //strtoul set EINVAL(after C99) if the string is invalid
unsigned long appid = strtoul(appIdStr, NULL, 10); unsigned long appid = strtoul(appIdStr, &strtoulSentinel, 10);
if(errno == EINVAL) { if(errno == EINVAL || strtoulSentinel == appIdStr) {
printf("Appid has to be a valid number\n"); printf("Appid has to be a valid number\n");
return -1; return -1;
} }
int gameId = getGameIdFromAppId(appid); int gameId = getGameIdFromAppId((int)appid);
if(gameId < 0) { if(gameId < 0) {
printf("Game is not compatible\n"); printf("Game is not compatible\n");
return -1; return -1;
@@ -500,9 +501,20 @@ int swapMod(int argc, char ** argv) {
} }
char * sentinel;
//no mod will go over the MAX_INT value //no mod will go over the MAX_INT value
int modIdA = (int)strtoul(argv[3], NULL, 10); int modIdA = (int)strtoul(argv[3], &sentinel, 10);
int modIdB = (int)strtoul(argv[4], NULL, 10); if(errno == EINVAL || sentinel == argv[3]) {
printf("Modid A has to be a valid number\n");
return -1;
}
int modIdB = (int)strtoul(argv[4], &sentinel, 10);
if(errno == EINVAL || sentinel == argv[4]) {
printf("Modid B has to be a valid number\n");
return -1;
}
printf("%d, %d\n", modIdA, modIdB);
return swapPlace(appid, modIdA, modIdB); return swapPlace(appid, modIdA, modIdB);
} }