From f61bb61aeba3e67fed9e4184df76da47f1406530 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Fri, 5 Oct 2012 01:59:04 +0000 Subject: [PATCH] Channels: Properly check for arguments to chanadd/chanrem HashObject: Fix potential divide by zero compiler_vm: Limit cpu usage and kill processes using excess cpu Unbuffer stdout --- PBot/AntiFlood.pm | 1 - PBot/Channels.pm | 4 +- PBot/HashObject.pm | 2 +- PBot/VERSION.pm | 4 +- modules/compiler_vm/compiler_server.pl | 18 ++++-- modules/compiler_vm/compiler_vm_client.pl | 7 +- modules/compiler_vm/compiler_watchdog.pl | 5 ++ modules/compiler_vm/prelude.h | 3 +- modules/gencstd.pl | 78 ++++++++++++----------- 9 files changed, 71 insertions(+), 51 deletions(-) diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index 1f5d5bed..c8b0b0f1 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -379,7 +379,6 @@ sub prune_message_history { foreach my $mask (keys %{ $self->{message_history} }) { foreach my $channel (keys %{ $self->{message_history}->{$mask}->{channels} }) { - $self->{pbot}->logger->log("Checking [$mask][$channel]\n"); my $length = $#{ $self->{message_history}->{$mask}->{channels}->{$channel}{messages} } + 1; next unless $length > 0; my %last = %{ @{ $self->{message_history}->{$mask}->{channels}->{$channel}{messages} }[$length - 1] }; diff --git a/PBot/Channels.pm b/PBot/Channels.pm index 83d685b3..06132f2f 100644 --- a/PBot/Channels.pm +++ b/PBot/Channels.pm @@ -70,7 +70,7 @@ sub unset { sub add { my ($self, $from, $nick, $user, $host, $arguments) = @_; - if(not defined $arguments) { + if(not defined $arguments or not length $arguments) { return "/msg $nick Usage: chanadd "; } @@ -84,7 +84,7 @@ sub add { sub remove { my ($self, $from, $nick, $user, $host, $arguments) = @_; - if(not defined $arguments) { + if(not defined $arguments or not length $arguments) { return "/msg $nick Usage: chanrem "; } diff --git a/PBot/HashObject.pm b/PBot/HashObject.pm index f8ee0497..cb446398 100644 --- a/PBot/HashObject.pm +++ b/PBot/HashObject.pm @@ -194,7 +194,7 @@ sub levenshtein_matches { # print "Percentage: ", $distance / $length, "\n"; - if($distance / $length < 0.50) { + if($length != 0 && $distance / $length < 0.50) { $result .= $comma . $index; $comma = ", "; } diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 757f7109..10806402 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,8 +13,8 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 385, - BUILD_DATE => "2012-09-17", + BUILD_REVISION => 386, + BUILD_DATE => "2012-10-04", }; 1; diff --git a/modules/compiler_vm/compiler_server.pl b/modules/compiler_vm/compiler_server.pl index df22a5db..201413e3 100755 --- a/modules/compiler_vm/compiler_server.pl +++ b/modules/compiler_vm/compiler_server.pl @@ -101,7 +101,7 @@ sub execute { } else { waitpid($child, 0); my $result = $? >> 8; - print "child exited, parent continuing\n"; + print "child exited, parent continuing [result = $result]\n"; return (undef, $result); } } @@ -117,7 +117,8 @@ sub compiler_server { my $hostinfo = gethostbyaddr($client->peeraddr); print '-' x 20, "\n"; printf "[Connect from %s]\n", $client->peerhost; - my $timed_out; + my $timed_out = 0; + my $killed = 0; eval { my $lang; @@ -144,19 +145,29 @@ sub compiler_server { if(not defined $ret) { #print "parent continued\n"; + print "parent continued [$result]\n"; $timed_out = 1 if $result == 243; # -13 == 243 + $killed = 1 if $result == 242; # -14 = 242 last; } $result =~ s/\s+$//; print "Ret: $ret; result: [$result]\n"; + if($result =~ m/Killed$/) { + print "Processed was killed\n"; + $killed = 1; + } + if($ret == -13) { print $client "$nick: "; } print $client $result . "\n"; close $client; + + $ret = -14 if $killed; + # child exit # print "child exit\n"; exit $ret; @@ -170,7 +181,6 @@ sub compiler_server { } $code .= $line . "\n"; - } alarm 0; @@ -180,7 +190,7 @@ sub compiler_server { close $client; - next unless $timed_out; + next unless ($timed_out or $killed); print "stopping vm $vm_pid\n"; vm_stop $vm_pid; diff --git a/modules/compiler_vm/compiler_vm_client.pl b/modules/compiler_vm/compiler_vm_client.pl index 97252b92..5fb8d737 100755 --- a/modules/compiler_vm/compiler_vm_client.pl +++ b/modules/compiler_vm/compiler_vm_client.pl @@ -728,8 +728,6 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') { $prelude .= $1; } - #$prelude = "$1" if $precode =~ s/^\s*(#.*\n)//s; - print "*** prelude: [$prelude]\n precode: [$precode]\n" if $debug; # strip C and C++ style comments @@ -806,7 +804,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') { $precode =~ s/^{(.*)}$/$1/s; if(not $has_main) { - $code = "$prelude\n\n$code\n\nint main(int argc, char **argv) {\n$precode\n;\nreturn 0;\n}\n"; + $code = "$prelude\n\n$code\n\nint main(void) {\n$precode\n;\nreturn 0;\n}\n"; $nooutput = "No warnings, errors or output."; } else { print "code: [$code]; precode: [$precode]\n" if $debug; @@ -897,7 +895,8 @@ if($output =~ m/^\s*$/) { $output =~ s/, //g; $output =~ s/\s*warning: shadowed declaration is here \[-Wshadow\]//g; $output =~ s/preprocessor macro>\s+/preprocessor macro>/g; - $output =~ s/\s*//; + $output =~ s/\s*//g; + $output =~ s/cc1: all warnings being treated as; errors//g; # remove duplicate warnings/infos $output =~ s/(\[*.*warning:.*?\s*)\1/$1/g; diff --git a/modules/compiler_vm/compiler_watchdog.pl b/modules/compiler_vm/compiler_watchdog.pl index 50d74d75..497093c3 100755 --- a/modules/compiler_vm/compiler_watchdog.pl +++ b/modules/compiler_vm/compiler_watchdog.pl @@ -358,6 +358,11 @@ sub execute { exit 0; } + if($line =~ m/Program terminated with signal SIGKILL/) { + print "Killed\n"; + return 0; + } + if($line =~ m/Program received signal SIGTRAP/) { my $output = ""; my $line = <$out>; diff --git a/modules/compiler_vm/prelude.h b/modules/compiler_vm/prelude.h index 0e86fb52..978aac58 100644 --- a/modules/compiler_vm/prelude.h +++ b/modules/compiler_vm/prelude.h @@ -83,6 +83,7 @@ static int printf_binary_arginfo(const struct printf_info *info, size_t n, int * __attribute__ (( constructor )) static void printf_binary_register(void) { + setvbuf(stdout, NULL, _IONBF, 0); setlocale(LC_ALL, ""); register_printf_specifier('b', printf_binary_handler, printf_binary_arginfo); } @@ -92,7 +93,7 @@ __attribute__ (( constructor )) static void printf_binary_register(void) #define STR(s) #s #define REVEAL(s) STR(s) -void gdb() { asm(""); } +void gdb() { __asm__(""); } #define dump(expression) gdb("print " #expression) #define print(expression) gdb("print " #expression) #define ptype(expression) gdb("ptype " #expression) diff --git a/modules/gencstd.pl b/modules/gencstd.pl index aaba364c..49027aff 100755 --- a/modules/gencstd.pl +++ b/modules/gencstd.pl @@ -8,7 +8,7 @@ use strict; use HTML::Entities; use Data::Dumper; -my $debug = 9999; +my $debug = 1000; sub gen_data; sub gen_txt; @@ -34,8 +34,8 @@ my $footnote = 0; my $last_footnote = 0; gen_data; -gen_txt; -#gen_html; +#gen_txt; +gen_html; sub gen_data { while($text =~ m/^\s{0,5}([0-9A-Z]+\.[0-9\.]*)/msg) { @@ -45,11 +45,11 @@ sub gen_data { ($section_number) = $this_section =~ /([^.]+)\./; - print STDERR "----------------------------------\n" if $debug >= 2; + print STDERR "----------------------------------\n" if $debug; print STDERR "Processing section [$this_section]; number [$section_number]\n" if $debug; - print STDERR "this_section: [$this_section]; last_section: [$last_section]\n"; - print STDERR "Section diff: ", ($this_section - $last_section), "\n"; + print STDERR "this_section: [$this_section]; last_section: [$last_section]\n" if $debug >= 2; + print STDERR "Section diff: ", ($this_section - $last_section), "\n" if $debug >= 2; my $diff = $section_number - $last_section_number; print STDERR "Diff: $diff\n" if $debug >= 2; @@ -64,7 +64,7 @@ sub gen_data { if($text =~ m/(.*?)^(?=\s{0,4}[0-9A-Z]+\.)/msg) { $section_text = $1; } else { - print STDERR "No section text, end of file marker found.\n" if $debug >= 4; + print STDERR "No section text, end of file marker found.\n"; last; } @@ -100,12 +100,12 @@ sub gen_data { $footnote = $2; my $middle_spaces = $3; my $footnote_text = "$4\n"; - print STDERR "1st footnote\n"; + print STDERR "1st footnote\n" if $debug; print STDERR "processing footnote $footnote [last: $last_footnote]\n" if $debug >= 2; if($last_footnote - $footnote != -1) { - print STDERR "footnotes dump: \n"; + print STDERR "footnotes dump: \n" if $debug > 5; shift @footnotes; - my $dump = Dumper(@footnotes); + my $dump = Dumper(@footnotes) if $debug > 5; #print STDERR "$dump\n"; die "Footnote diff invalid"; } @@ -118,26 +118,26 @@ sub gen_data { while ($t =~ m/^(.*?)$/msgc) { my $line = $1; - print STDERR "processing [$line]\n"; + print STDERR "processing [$line]\n" if $debug; if($line =~ m/^(\s*)(\d+)\)(\s*)(.*?)$/msg) { - print STDERR "----------------\n" if $debug >= 10; - print STDERR "footnote $footnote: [$footnote_text]\n" if $debug >= 5; + print STDERR "----------------\n" if $debug >= 1; + print STDERR "footnote $footnote: [$footnote_text]\n" if $debug >= 1; $footnotes[$footnote] = $footnote_text; - print STDERR "----------------\n" if $debug >= 10; + print STDERR "----------------\n" if $debug >= 1; $leading_spaces = $1; $footnote = $2; $middle_spaces = $3; $footnote_text = "$4\n"; - print STDERR "2nd footnote\n"; + print STDERR "2nd footnote\n" if $debug >= 2; print STDERR "processing footnote $footnote [last: $last_footnote]\n" if $debug >= 2; if($last_footnote - $footnote != -1) { print STDERR "footnotes dump: \n"; shift @footnotes; my $dump = Dumper(@footnotes); - #print STDERR "$dump\n"; + print STDERR "$dump\n" if $debug >= 3; die "Footnote diff invalid"; } $last_footnote = $footnote; @@ -157,10 +157,10 @@ sub gen_data { print STDERR "footnote $footnote text: appending [$line]\n" if $debug >= 3; } - print STDERR "----------------\n" if $debug >= 10; - print STDERR "footnote $footnote: [$footnote_text]\n" if $debug >= 5; + print STDERR "----------------\n" if $debug >= 1; + print STDERR "footnote $footnote: [$footnote_text]\n" if $debug >= 1; $footnotes[$footnote] = $footnote_text; - print STDERR "----------------\n" if $debug >= 10; + print STDERR "----------------\n" if $debug >= 1; } $sections{$this_section . "p$p"}{text} = "$p $t"; @@ -171,7 +171,7 @@ sub gen_data { sub bysection { my $inverse = 1; - print STDERR "section cmp $a <=> $b\n"; + print STDERR "section cmp $a <=> $b\n" if $debug > 10; my ($a1, $p1) = split /p/, $a; my ($b1, $p2) = split /p/, $b; @@ -210,22 +210,22 @@ sub bysection { if(not defined $k2[$i]) { $r[$i] = 1; } else { - print STDERR " cmp k1[$i] ($k1[$i]) vs k2[$i] ($k2[$i])\n"; + print STDERR " cmp k1[$i] ($k1[$i]) vs k2[$i] ($k2[$i])\n" if $debug >= 5; if($i == 0) { $r[$i] = $k1[$i] cmp $k2[$i]; } else { $r[$i] = $k1[$i] <=> $k2[$i]; } } - print STDERR " r[$i] = $r[$i]\n"; + print STDERR " r[$i] = $r[$i]\n" if $debug >= 5; } $r[$i] = ($p1 <=> $p2); - print STDERR " $p1 <=> $p2 => r[$i] = $r[$i]\n"; + print STDERR " $p1 <=> $p2 => r[$i] = $r[$i]\n" if $debug >= 5; my $ret = 0; foreach my $rv (@r) { - print STDERR " checking r: $rv\n"; + print STDERR " checking r: $rv\n" if $debug >= 5; if($rv != 0) { $ret = $rv; last; @@ -234,7 +234,7 @@ sub bysection { $ret = $ret * $inverse; - print STDERR "ret $ret\n"; + print STDERR "ret $ret\n" if $debug >= 5; return $ret; } @@ -245,7 +245,7 @@ sub gen_txt { my $section_title; foreach my $this_section (sort bysection keys %sections) { - print STDERR "writing section $this_section\n"; + print STDERR "writing section $this_section\n" if $debug; if(not $this_section =~ m/p/) { print " $this_section $sections{$this_section}{title}\n"; $section_head = $this_section; @@ -264,7 +264,7 @@ sub gen_txt { while($section_text =~ m/^(.*?)$/msg) { my $line = $1; - print STDERR "paren reset, line [$line]\n"; + print STDERR "paren reset, line [$line]\n" if $debug >= 8; my $number = ""; while($line =~ m/(.)/g) { my $c = $1; @@ -275,14 +275,14 @@ sub gen_txt { $number = ""; } elsif($c eq '(') { $paren++; - print STDERR "got $paren (\n"; + print STDERR "got $paren (\n" if $debug >= 8; } elsif($c eq ')') { $paren--; - print STDERR "got $paren )\n"; + print STDERR "got $paren )\n" if $debug >= 8; if($paren == -1) { if(length $number and defined $footnotes[$number]) { - print STDERR "Got footnote $number here!\n"; + print STDERR "Got footnote $number here!\n" if $debug; $footer .= " FOOTNOTE.$number\n $footnotes[$number]\n"; } @@ -310,7 +310,7 @@ sub gen_html { my $paren = 0; foreach my $this_section (sort bysection keys %sections) { - print STDERR "writing section $this_section\n"; + print STDERR "writing section $this_section\n" if $debug; print "\n"; print "
\n

", encode_entities $this_section, " [", encode_entities $sections{$this_section}{title}, "]

\n" if not $this_section =~ m/p/; @@ -323,10 +323,12 @@ sub gen_html { $section_text =~ s/^\s*$footnote\)\s*$sub//ms; } + $section_text = encode_entities $section_text; + while($section_text =~ m/^(.*?)$/msg) { my $line = $1; - print STDERR "paren reset, line [$line]\n"; + print STDERR "paren reset, line [$line]\n" if $debug >= 8; my $number = ""; while($line =~ m/(.)/g) { my $c = $1; @@ -337,14 +339,15 @@ sub gen_html { $number = ""; } elsif($c eq '(') { $paren++; - print STDERR "got $paren (\n"; + print STDERR "got $paren (\n" if $debug >= 8; } elsif($c eq ')') { $paren--; - print STDERR "got $paren )\n"; + print STDERR "got $paren )\n" if $debug >= 8; if($paren == -1) { if(length $number and defined $footnotes[$number]) { - print STDERR "Got footnote $number here!\n"; + print STDERR "Got footnote $number here!\n" if $debug; + $section_text =~ s/$number\)/[$number]<\/sup>/; $footer .= "
\n
Footnote $number) ", encode_entities $footnotes[$number], "
\n
\n"; } @@ -356,7 +359,10 @@ sub gen_html { } } - print "
", encode_entities $section_text, "
\n"; + $section_text =~ s/\(([0-9.]+)\)/($1<\/a>)/g; + $footer =~ s/\(([0-9.]+)\)/($1<\/a>)/g; + + print "
", $section_text, "
\n"; print "
\n"; if(length $footer) {