Skip site navigation (1) Skip section navigation (2)

[PATCH] Teach Catalog.pm how many attributes there should be per DATA() line

From: David Christensen <david(at)endpoint(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] Teach Catalog.pm how many attributes there should be per DATA() line
Date: 2017-02-15 15:40:18
Message-ID: 20170215154018.fs5vwtqhp5d2sifs@veeddeux.attlocal.net (view raw, whole thread or download thread mbox)
Thread:
Lists: pgsql-hackers
Throws a build error if we encounter a different number of fields in a
DATA() line than we expect for the catalog in question.

Previously, it was possible to silently ignore any mismatches at build
time which could result in symbol undefined errors at link time.  Now
we stop and identify the infringing line as soon as we encounter it,
which greatly speeds up the debugging process.
---
 src/backend/catalog/Catalog.pm   | 26 +++++++++++++++++++++++++-
 src/backend/utils/Gen_fmgrtab.pl | 18 ++++++++++++++++--
 2 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index e1f3c3a..86f5b59 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -46,6 +46,9 @@ sub Catalogs
 
 		open(INPUT_FILE, '<', $input_file) || die "$input_file: $!";
 
+		my ($filename) = ($input_file =~ m/(\w+)\.h$/);
+		my $natts_pat = "Natts_$filename";
+
 		# Scan the input file.
 		while (<INPUT_FILE>)
 		{
@@ -70,8 +73,15 @@ sub Catalogs
 			s/\s+/ /g;
 
 			# Push the data into the appropriate data structure.
-			if (/^DATA\(insert(\s+OID\s+=\s+(\d+))?\s+\(\s*(.*)\s*\)\s*\)$/)
+			if (/$natts_pat\s+(\d+)/)
+			{
+				$catalog{natts} = $1;
+			}
+			elsif (/^DATA\(insert(\s+OID\s+=\s+(\d+))?\s+\(\s*(.*)\s*\)\s*\)$/)
 			{
+				check_natts($filename, $catalog{natts},$3) or
+				  die sprintf "Wrong number of Natts in DATA() line %s:%d\n", $input_file, INPUT_FILE->input_line_number;
+
 				push @{ $catalog{data} }, { oid => $2, bki_values => $3 };
 			}
 			elsif (/^DESCR\(\"(.*)\"\)$/)
@@ -216,4 +226,18 @@ sub RenameTempFile
 	rename($temp_name, $final_name) || die "rename: $temp_name: $!";
 }
 
+# verify the number of fields in the passed-in bki structure
+sub check_natts
+{
+	my ($catname, $natts, $bki_val) = @_;
+	unless ($natts)
+	{
+		die "Could not find definition for Natts_${catname} before start of DATA()\n";
+	}
+
+	# we're working with a copy and need to count the fields only, so collapse
+	$bki_val =~ s/"[^"]*?"/xxx/g;
+
+	return (split /\s+/ => $bki_val) == $natts;
+}
 1;
diff --git a/src/backend/utils/Gen_fmgrtab.pl b/src/backend/utils/Gen_fmgrtab.pl
index cdd603a..49a5d80 100644
--- a/src/backend/utils/Gen_fmgrtab.pl
+++ b/src/backend/utils/Gen_fmgrtab.pl
@@ -56,9 +56,11 @@ foreach my $column (@{ $catalogs->{pg_proc}->{columns} })
 }
 
 my $data = $catalogs->{pg_proc}->{data};
+my $natts = $catalogs->{pg_proc}->{natts};
+my $elem = 0;
+
 foreach my $row (@$data)
 {
-
 	# To construct fmgroids.h and fmgrtab.c, we need to inspect some
 	# of the individual data fields.  Just splitting on whitespace
 	# won't work, because some quoted fields might contain internal
@@ -67,7 +69,19 @@ foreach my $row (@$data)
 	# fields that might need quoting, so this simple hack is
 	# sufficient.
 	$row->{bki_values} =~ s/"[^"]*"/"xxx"/g;
-	@{$row}{(at)attnames} = split /\s+/, $row->{bki_values};
+    my @bki_values = split /\s+/, $row->{bki_values};
+
+    # verify we've got the expected number of data fields
+    if (@bki_values != $natts)
+    {
+        die sprintf <<EOF, $elem, $natts, scalar @bki_values;
+Wrong number of attributes in pg_proc DATA() entry %d (expected %d but got %d)
+EOF
+    }
+    $elem++;
+    
+	@{$row}{(at)attnames} = @bki_values;
+
 
 	# Select out just the rows for internal-language procedures.
 	# Note assumption here that INTERNALlanguageId is 12.
-- 
2.8.4 (Apple Git-73)



Responses

pgsql-hackers by date

Next:From: David ChristensenDate: 2017-02-15 15:40:49
Subject: [PATCH] Fix pg_proc comment grammar
Previous:From: Jim NasbyDate: 2017-02-15 15:37:49
Subject: Re: Reporting xmin from VACUUMs

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group