[go: nahoru, domu]

Skip to content

Commit

Permalink
Add more validation to mcrcon_parse_seconds()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiiffi committed Dec 19, 2019
1 parent 5fb20c2 commit 4f3a455
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions mcrcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,21 @@ void sighandler(/*int sig*/)

unsigned int mcrcon_parse_seconds(char *str)
{
long result = strtol(str, NULL, 10);
char *end;
long result = strtol(str, &end, 10);

if (errno != 0) {
fprintf(stderr, "-w invalid value.\nerror %d: %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}

if (end == str) {
fprintf(stderr, "-w invalid value (not a number?)\n");
exit(EXIT_FAILURE);
}

if (result <= 0 || result > MAX_WAIT_TIME) {
fprintf(stderr, "-w option value is out of range.\nAcceptable value is 1 - %d seconds.\n", MAX_WAIT_TIME);
fprintf(stderr, "-w value out of range.\nAcceptable value is 1 - %d (seconds).\n", MAX_WAIT_TIME);
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -178,10 +184,6 @@ int main(int argc, char *argv[])
{
switch (opt)
{
case '?':
puts("Try 'mcrcon -h' or 'man mcrcon' for help.\n");
exit(EXIT_FAILURE);

case 'H': host = optarg; break;
case 'P': port = optarg; break;
case 'p': pass = optarg; break;
Expand All @@ -195,17 +197,19 @@ int main(int argc, char *argv[])
break;

case 'v':
puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon\n");
puts(VER_STR"\nhttps://github.com/Tiiffi/mcrcon");
exit(EXIT_SUCCESS);

case 'h': usage(); break;

default: exit(EXIT_FAILURE);
case '?':
default:
puts("Try 'mcrcon -h' or 'man mcrcon' for help.");
exit(EXIT_FAILURE);
}
}

if (pass == NULL) {
puts("You must give password (-p password). Try 'mcrcon -h' or 'man mcrcon' for help.\n");
puts("You must give password (-p password).\nTry 'mcrcon -h' or 'man mcrcon' for help.");
return 0;
}

Expand Down Expand Up @@ -261,7 +265,7 @@ void usage(void)
" -s\t\tSilent mode\n"
" -c\t\tDisable colors\n"
" -r\t\tOutput raw packets\n"
" -w\t\tWait for specified duration (1 - 600 seconds) between each command\n"
" -w\t\tWait for specified duration (seconds) between each command (1 - 600s)\n"
" -h\t\tPrint usage\n"
" -v\t\tVersion information\n\n"
"Server address, port and password can be set with following environment variables:\n"
Expand All @@ -275,7 +279,7 @@ void usage(void)
puts("- Command-line options will override environment variables");
puts("- Rcon commands with spaces must be enclosed in quotes\n");
puts("Example:\n\t"IN_NAME" -H my.minecraft.server -p password -w 5 \"say Server is restarting!\" save-all stop\n");
puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/\n");
puts(VER_STR"\nReport bugs to tiiffi+mcrcon at gmail or https://github.com/Tiiffi/mcrcon/issues/");

#ifdef _WIN32
puts("Press enter to exit.");
Expand Down

0 comments on commit 4f3a455

Please sign in to comment.