Skip to content

Commit 02c0eb6

Browse files
author
Bryan Henderson
committed
Properly structure ProcedureNameIndexScan so it doesn't generate
"may be used before being set" warnings.
1 parent bf14017 commit 02c0eb6

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

src/backend/catalog/indexing.c

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.6 1996/11/13 20:47:57 scrappy Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/indexing.c,v 1.7 1996/11/26 02:45:05 bryanh Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -352,26 +352,36 @@ ProcedureOidIndexScan(Relation heapRelation, Oid procId)
352352
return tuple;
353353
}
354354

355+
356+
355357
HeapTuple
356358
ProcedureNameIndexScan(Relation heapRelation,
357-
char *procName,
358-
int nargs,
359-
Oid *argTypes)
359+
char *procName,
360+
int nargs,
361+
Oid *argTypes)
360362
{
361363
Relation idesc;
362364
ScanKeyData skey;
363-
HeapTuple tuple;
365+
HeapTuple tuple; /* tuple being tested */
366+
HeapTuple return_tuple; /* The tuple pointer we eventually return */
364367
IndexScanDesc sd;
365368
RetrieveIndexResult indexRes;
366369
Buffer buffer;
367370
Form_pg_proc pgProcP;
368-
bool bufferUsed = FALSE;
371+
bool ScanComplete;
372+
/* The index scan is complete, i.e. we've scanned everything there
373+
is to scan.
374+
*/
375+
bool FoundMatch;
376+
/* In scanning pg_proc, we have found a row that meets our search
377+
criteria.
378+
*/
369379

370380
ScanKeyEntryInitialize(&skey,
371-
(bits16)0x0,
372-
(AttrNumber)1,
373-
(RegProcedure)NameEqualRegProcedure,
374-
(Datum)procName);
381+
(bits16)0x0,
382+
(AttrNumber)1,
383+
(RegProcedure)NameEqualRegProcedure,
384+
(Datum)procName);
375385

376386
idesc = index_openr(ProcedureNameIndex);
377387

@@ -382,41 +392,46 @@ ProcedureNameIndexScan(Relation heapRelation,
382392
* by hand, so that we can check that the other keys match. when
383393
* multi-key indices are added, they will be used here.
384394
*/
385-
do {
386-
tuple = (HeapTuple)NULL;
387-
if (bufferUsed) {
388-
ReleaseBuffer(buffer);
389-
bufferUsed = FALSE;
390-
}
391-
392-
indexRes = index_getnext(sd, ForwardScanDirection);
393-
if (indexRes) {
394-
ItemPointer iptr;
395-
396-
iptr = &indexRes->heap_iptr;
397-
tuple = heap_fetch(heapRelation, NowTimeQual, iptr, &buffer);
398-
pfree(indexRes);
399-
if (HeapTupleIsValid(tuple)) {
400-
pgProcP = (Form_pg_proc)GETSTRUCT(tuple);
401-
bufferUsed = TRUE;
402-
}
403-
} else
404-
break;
405-
} while (!HeapTupleIsValid(tuple) ||
406-
pgProcP->pronargs != nargs ||
407-
!oid8eq(&(pgProcP->proargtypes[0]), argTypes));
408-
409-
if (HeapTupleIsValid(tuple)) {
410-
tuple = heap_copytuple(tuple);
411-
ReleaseBuffer(buffer);
395+
tuple = (HeapTuple) NULL; /* initial value */
396+
ScanComplete = false; /* Scan hasn't begun yet */
397+
FoundMatch = false; /* No match yet; haven't even looked. */
398+
while (!FoundMatch && !ScanComplete) {
399+
indexRes = index_getnext(sd, ForwardScanDirection);
400+
if (indexRes) {
401+
ItemPointer iptr;
402+
403+
iptr = &indexRes->heap_iptr;
404+
tuple = heap_fetch(heapRelation, NowTimeQual, iptr, &buffer);
405+
pfree(indexRes);
406+
if (HeapTupleIsValid(tuple)) {
407+
/* Here's a row for a procedure that has the sought procedure
408+
name. To be a match, though, we need it to have the
409+
right number and type of arguments too, so we check that
410+
now.
411+
*/
412+
pgProcP = (Form_pg_proc)GETSTRUCT(tuple);
413+
if (pgProcP->pronargs == nargs &&
414+
oid8eq(&(pgProcP->proargtypes[0]), argTypes))
415+
FoundMatch = true;
416+
else ReleaseBuffer(buffer);
417+
}
418+
} else ScanComplete = true;
412419
}
420+
421+
if (FoundMatch) {
422+
Assert(HeapTupleIsValid(tuple));
423+
return_tuple = heap_copytuple(tuple);
424+
ReleaseBuffer(buffer);
425+
} else return_tuple = (HeapTuple)NULL;
413426

414427
index_endscan(sd);
415428
index_close(idesc);
416429

417-
return tuple;
430+
return return_tuple;
418431
}
419432

433+
434+
420435
HeapTuple
421436
ProcedureSrcIndexScan(Relation heapRelation, text *procSrc)
422437
{

0 commit comments

Comments
 (0)