Skip to contentSkip to conversation

pan/ltp-pan.c: Add file parsing protection

PATCH Superseded Dylan Chung 2 participants 2 messages Nov 15, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pan/ltp-pan.c: Add file parsing protection

A segmentation fault occurs given the wrong format in test cases file. for example:

    name  command line
    ----- -----------------
    dio10 diotest4 -b 65536    (O)
    dio10                      (X)
    dio10    diotest4 -b 65536 (X)

This patch adds protection and print warning when the command part of the tests is empty or multiple whitespaces.

---
 pan/ltp-pan.c | 8 ++++++++
 1 file changed, 8 insertions(+)
pan/ltp-pan.c +8 -0
index 8ac484ec6..470bcbfc5 100644
@@ -1200,6 +1200,14 @@ static struct collection *get_collection(char *file, int optind, int argc,
 				n->pcnt_f[1] = 's';
 			}
 			n->name = strdup(strsep(&a, " \t"));
+			while (a != NULL && isspace(*a))
+				a++;
+			if (a == NULL || strlen(a) == 0) {
+				fprintf(stderr,
+					"pan(%s): Testcase '%s' requires a command to execute.\n",
+					panname, n->name);
+				return 0;
+			}
 			n->cmdline = strdup(a);
 			n->next = NULL;
 
Signature · 1 line
2.17.0
#1 lore raw

PATCH 1/1 pan/ltp-pan.c: Add file parsing protection

Hi!

Quoted 9 lines

A segmentation fault occurs given the wrong format in test cases file. for example:

    name  command line
    ----- -----------------
    dio10 diotest4 -b 65536    (O)
    dio10                      (X)
    dio10    diotest4 -b 65536 (X)

This patch adds protection and print warning when the command part of the tests is empty or multiple whitespaces.

And the expected action here is that we return empty collection when the file is wrongly formatted and ltp-pan aborts, right?

You should probably write something along these lines here as well.

Quoted 10 lines
---
 pan/ltp-pan.c | 8 ++++++++
 1 file changed, 8 insertions(+)
pan/ltp-pan.c +0 -0
index 8ac484ec6..470bcbfc5 100644

pan/ltp-pan.c static struct collection *get_collection(char *file, int optind, int argc, · line 1205

Quoted 6 lines
pan/ltp-pan.c +3 -0
@@ -1200,6 +1200,14 @@ static struct collection *get_collection(char *file, int optind, int argc,
 				n->pcnt_f[1] = 's';
 			}
 			n->name = strdup(strsep(&a, " \t"));
+			while (a != NULL && isspace(*a))
+				a++;
+			if (a == NULL || strlen(a) == 0) {

^
Maybe just || a[0] == 0 there is no point in using strlen for checking for empty string...

pan/ltp-pan.c static struct collection *get_collection(char *file, int optind, int argc, · line 1209

Quoted 4 lines
pan/ltp-pan.c +4 -0
+				fprintf(stderr,
+					"pan(%s): Testcase '%s' requires a command to execute.\n",
+					panname, n->name);
+				return 0;

^
This should be NULL

pan/ltp-pan.c static struct collection *get_collection(char *file, int optind, int argc, · line 1212

Quoted 3 lines
pan/ltp-pan.c +1 -0
+			}
 			n->cmdline = strdup(a);
 			n->next = NULL;

Otherwise it looks good.

Signature · 2 lines
Cyril Hrubis
[email protected]