From 7629179cc5c88d8207a43282134d3d767121ce39 Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Mon, 4 Dec 2006 10:32:48 +0000 Subject: - much stricter synthesis parsing. fail on first error - add test using a buggy synthesis that occured on kenobi --- URPM.xs | 19 ++++++++++++++----- t/buggy_synthesis.cz | Bin 0 -> 40711 bytes t/empty_synthesis.cz | Bin 0 -> 20 bytes t/synthesis.t | 27 +++++++++++++++++---------- 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 t/buggy_synthesis.cz create mode 100644 t/empty_synthesis.cz diff --git a/URPM.xs b/URPM.xs index 6ea6dca..3704b40 100644 --- a/URPM.xs +++ b/URPM.xs @@ -979,14 +979,16 @@ call_package_callback(SV *urpm, SV *sv_pkg, SV *callback) { return sv_pkg != NULL; } -static void +static int parse_line(AV *depslist, HV *provides, URPM__Package pkg, char *buff, SV *urpm, SV *callback) { SV *sv_pkg; URPM__Package _pkg; char *tag, *data; int data_len; - if ((tag = strchr(buff, '@')) != NULL && (data = strchr(tag+1, '@')) != NULL) { + if (buff[0] == 0) { + return 1; + } else if ((tag = buff)[0] == '@' && (data = strchr(tag+1, '@')) != NULL) { *tag++ = *data++ = 0; data_len = 1+strlen(data); if (!strcmp(tag, "info")) { @@ -1011,6 +1013,10 @@ parse_line(AV *depslist, HV *provides, URPM__Package pkg, char *buff, SV *urpm, } else if (!strcmp(tag, "summary")) { free(pkg->summary); pkg->summary = memcpy(malloc(data_len), data, data_len); } + return 1; + } else { + fprintf(stderr, "bad line <%s>\n", buff); + return 0; } } @@ -3031,6 +3037,7 @@ Urpm_parse_synthesis__XS(urpm, filename, ...) memset(&pkg, 0, sizeof(struct s_Package)); buff[sizeof(buff)-1] = 0; p = buff; + int ok = 1; while ((buff_len = gzread(f, p, sizeof(buff)-1-(p-buff))) >= 0 && (buff_len += p-buff)) { buff[buff_len] = 0; @@ -3038,15 +3045,17 @@ Urpm_parse_synthesis__XS(urpm, filename, ...) if ((eol = strchr(p, '\n')) != NULL) { do { *eol++ = 0; - parse_line(depslist, provides, &pkg, p, urpm, callback); + if (!parse_line(depslist, provides, &pkg, p, urpm, callback)) { ok = 0; break; } p = eol; } while ((eol = strchr(p, '\n')) != NULL); } else { /* a line larger than sizeof(buff) has been encountered, bad file problably */ + fprintf(stderr, "invalid line <%s>\n", p); + ok = 0; break; } if (gzeof(f)) { - parse_line(depslist, provides, &pkg, p, urpm, callback); + if (!parse_line(depslist, provides, &pkg, p, urpm, callback)) ok = 0; break; } else { /* move the remaining non-complete-line at beginning */ @@ -3055,7 +3064,7 @@ Urpm_parse_synthesis__XS(urpm, filename, ...) p = &buff[buff_len-(p-buff)]; } } - int ok = gzclose(f) == 0; + if (gzclose(f) != 0) ok = 0; SPAGAIN; if (ok) { XPUSHs(sv_2mortal(newSViv(start_id))); diff --git a/t/buggy_synthesis.cz b/t/buggy_synthesis.cz new file mode 100644 index 0000000..fa1f68d Binary files /dev/null and b/t/buggy_synthesis.cz differ diff --git a/t/empty_synthesis.cz b/t/empty_synthesis.cz new file mode 100644 index 0000000..8aee682 Binary files /dev/null and b/t/empty_synthesis.cz differ diff --git a/t/synthesis.t b/t/synthesis.t index 4050493..f44e769 100644 --- a/t/synthesis.t +++ b/t/synthesis.t @@ -2,19 +2,19 @@ use strict ; use warnings ; -use Test::More tests => 86; +use Test::More tests => 88; use URPM; my $file1 = 'synthesis.sample.cz'; open my $f, "| gzip -9 >$file1"; -print $f q{ -glibc-devel@provides@glibc-devel == 6:2.2.4-25mdk -glibc-devel@requires@/sbin/install-info@glibc == 2.2.4@kernel-headers@kernel-headers >= 2.2.1@/bin/sh@/bin/sh@/bin/sh@rpmlib(PayloadFilesHavePrefix) <= 4.0-1@rpmlib(CompressedFileNames) <= 3.0.4-1 -glibc-devel@conflicts@texinfo < 3.11@gcc < 2.96-0.50mdk -glibc-devel@obsoletes@libc-debug@libc-headers@libc-devel@linuxthreads-devel@glibc-debug -glibc-devel@info@glibc-devel-2.2.4-25mdk.i586@6@45692097@Development/C -}; +print $f <<'EOF'; +@provides@glibc-devel == 6:2.2.4-25mdk +@requires@/sbin/install-info@glibc == 2.2.4@kernel-headers@kernel-headers >= 2.2.1@/bin/sh@/bin/sh@/bin/sh@rpmlib(PayloadFilesHavePrefix) <= 4.0-1@rpmlib(CompressedFileNames) <= 3.0.4-1 +@conflicts@texinfo < 3.11@gcc < 2.96-0.50mdk +@obsoletes@libc-debug@libc-headers@libc-devel@linuxthreads-devel@glibc-debug +@info@glibc-devel-2.2.4-25mdk.i586@6@45692097@Development/C +EOF close $f; END { unlink $file1 } @@ -22,9 +22,16 @@ END { unlink $file1 } my $a = new URPM; ok($a); -my ($first, $end) = $a->parse_synthesis($file1); +my ($first, $end); + +($first, $end) = URPM->new->parse_synthesis('t/empty_synthesis.cz'); +is("$first $end", "0 -1", 'parse empty synthesis'); + +is(URPM->new->parse_synthesis('t/buggy_synthesis.cz'), undef, 'parse buggy synthesis'); + +($first, $end) = $a->parse_synthesis($file1); ok($first == 0 && $end == 0); -ok(@{$a->{depslist}} == 1); +is(int @{$a->{depslist}}, 1); ok(keys(%{$a->{provides}}) == 3); ok(defined $a->{provides}{'glibc-devel'}); ok(exists $a->{provides}{'/bin/sh'}); -- cgit v1.2.1