3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-01 17:16:39 +02:00

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
This commit is contained in:
Pragmatic Software 2012-10-05 01:59:04 +00:00
parent 65660625c2
commit f61bb61aeb
9 changed files with 71 additions and 51 deletions

View File

@ -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] };

View File

@ -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 <channel>";
}
@ -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 <channel>";
}

View File

@ -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 = ", ";
}

View File

@ -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;

View File

@ -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;

View File

@ -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/, <incomplete sequence >//g;
$output =~ s/\s*warning: shadowed declaration is here \[-Wshadow\]//g;
$output =~ s/preprocessor macro>\s+<at\s+>/preprocessor macro>/g;
$output =~ s/<No symbol table is loaded. Use the "file" command.>\s*//;
$output =~ s/<No symbol table is loaded. Use the "file" command.>\s*//g;
$output =~ s/cc1: all warnings being treated as; errors//g;
# remove duplicate warnings/infos
$output =~ s/(\[*.*warning:.*?\s*)\1/$1/g;

View File

@ -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>;

View File

@ -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)

View File

@ -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 "<a name='", encode_entities $this_section, "'>\n";
print "<hr>\n<h3>", encode_entities $this_section, " [", encode_entities $sections{$this_section}{title}, "]</h3>\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\)/<sup>[$number]<\/sup>/;
$footer .= "<a name='FOOTNOTE.$number'>\n<pre><i><b>Footnote $number)</b> ", encode_entities $footnotes[$number], "</i></pre>\n</a>\n";
}
@ -356,7 +359,10 @@ sub gen_html {
}
}
print "<pre>", encode_entities $section_text, "</pre>\n";
$section_text =~ s/\(([0-9.]+)\)/(<a href="#$1">$1<\/a>)/g;
$footer =~ s/\(([0-9.]+)\)/(<a href="#$1">$1<\/a>)/g;
print "<pre>", $section_text, "</pre>\n";
print "</a>\n";
if(length $footer) {