From 91ca9b44bc35aa16bec741cc15146d7dd5790d0d Mon Sep 17 00:00:00 2001 From: Alex Denes Date: Sun, 20 Dec 2020 11:08:53 +0000 Subject: [PATCH] Add one more test and improve parsing --- include/uirc.h | 2 +- src/tokenizers.c | 5 ++++- tests/CMakeLists.txt | 1 + tests/junk.c | 22 ++++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/junk.c diff --git a/include/uirc.h b/include/uirc.h index c855052..6cc336c 100644 --- a/include/uirc.h +++ b/include/uirc.h @@ -24,7 +24,7 @@ #ifndef UIRC_GUARD_UIRC #define UIRC_GUARD_UIRC -#define UIRC_VERSION 20201216 +#define UIRC_VERSION 20201220 #endif /* UIRC_GUARD_UIRC */ diff --git a/src/tokenizers.c b/src/tokenizers.c index 3944ad8..b83f226 100644 --- a/src/tokenizers.c +++ b/src/tokenizers.c @@ -43,12 +43,15 @@ Tok_mesg(char* str, IRC_Message* out) skip_spaces(&progr); - if ((command = strtok_mr(&progr, " ")) != NULL) { + if (isalnum(*progr) && (command = strtok_mr(&progr, " ")) != NULL) { signed short temp; if (isalpha(*command)) { if ((temp = Ircmd_stoi(command)) < UIRC_FCMD || temp > UIRC_LCMD) return ERR_UIRC_UNKNOWN_TOKEN; out->cmd = (IRC_Command) temp; } else { + for (size_t l = 0; command[l]; l++) { + if (!isdigit(command[l])) return ERR_UIRC_INVALID_FORMAT; + } out->cmd = (IRC_Command) atoi(command); } } else diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a592a13..f41cabb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,6 +14,7 @@ buildtest(NumericCmds numericmds) buildtest(IncorrectTrailing notrail) buildtest(SpacedArguments spacedargs) buildtest(StrTokMoveSave strtokmr) +buildtest(Junk junk) if ( BUILD_IRCV3 ) buildtest(TagParser tagtok) diff --git a/tests/junk.c b/tests/junk.c new file mode 100644 index 0000000..cc27585 --- /dev/null +++ b/tests/junk.c @@ -0,0 +1,22 @@ +#include "../include/uirc.h" + +#include +#include +#include + +int +main(void) +{ + char mesg[][513] = { + "", "NO ", "CM:", "@:", "@ ", "@ N", "@ N:", "\n", "\r", "AA x :\r", "9A x :\r", "XA x :\r", + }; + IRC_Message out = { 0 }; + for (unsigned long i = 0; i < sizeof(mesg) / sizeof(*mesg); i++) { + if (Tok_mesg(mesg[i], &out) == 1) { + printf("Message was parsed successfuly when it was junk.\nTest failed: #%lu %s\n", i, mesg[i]); + return EXIT_FAILURE; + } + } + return EXIT_SUCCESS; +} +