Skip to content

Commit cfbcb6b

Browse files
committed
Make the world safe for unsigned OIDs.
1 parent a70e74b commit cfbcb6b

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

contrib/lo/lo.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
/*
22
* PostgreSQL type definitions for managed LargeObjects.
33
*
4-
* $Id: lo.c,v 1.5 2000/11/20 20:36:55 tgl Exp $
4+
* $Header: /cvsroot/pgsql/contrib/lo/lo.c,v 1.6 2000/11/21 21:51:58 tgl Exp $
55
*
66
*/
77

8+
#include "postgres.h"
9+
810
#include <stdio.h>
911

10-
#include <postgres.h>
11-
#include <utils/palloc.h>
12+
#include "utils/palloc.h"
1213

1314
/* Required for largeobjects */
14-
#include <libpq/libpq-fs.h>
15-
#include <libpq/be-fsstubs.h>
15+
#include "libpq/libpq-fs.h"
16+
#include "libpq/be-fsstubs.h"
1617

1718
/* Required for SPI */
18-
#include <executor/spi.h>
19+
#include "executor/spi.h"
1920

2021
/* Required for triggers */
21-
#include <commands/trigger.h>
22+
#include "commands/trigger.h"
23+
24+
25+
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
2226

23-
/* required for tolower() */
2427

2528
/*
2629
* This is the internal storage format for managed large objects
@@ -40,7 +43,7 @@ Blob *lo(Oid oid); /* Return Blob based on oid */
4043
Datum lo_manage(PG_FUNCTION_ARGS); /* Trigger handler */
4144

4245
/*
43-
* This creates a large object, and set's its OID to the value in the
46+
* This creates a large object, and sets its OID to the value in the
4447
* supplied string.
4548
*
4649
* If the string is empty, then a new LargeObject is created, and its oid
@@ -55,20 +58,13 @@ lo_in(char *str)
5558

5659
if (strlen(str) > 0)
5760
{
58-
59-
count = sscanf(str, "%d", &oid);
61+
count = sscanf(str, "%u", &oid);
6062

6163
if (count < 1)
62-
{
6364
elog(ERROR, "lo_in: error in parsing \"%s\"", str);
64-
return (NULL);
65-
}
6665

67-
if (oid < 0)
68-
{
66+
if (oid == InvalidOid)
6967
elog(ERROR, "lo_in: illegal oid \"%s\"", str);
70-
return (NULL);
71-
}
7268
}
7369
else
7470
{
@@ -79,10 +75,7 @@ lo_in(char *str)
7975
oid = DatumGetObjectId(DirectFunctionCall1(lo_creat,
8076
Int32GetDatum(INV_READ | INV_WRITE)));
8177
if (oid == InvalidOid)
82-
{
8378
elog(ERROR, "lo_in: InvalidOid returned from lo_creat");
84-
return (NULL);
85-
}
8679
}
8780

8881
result = (Blob *) palloc(sizeof(Blob));
@@ -104,7 +97,7 @@ lo_out(Blob * addr)
10497
return (NULL);
10598

10699
result = (char *) palloc(32);
107-
sprintf(result, "%d", *addr);
100+
sprintf(result, "%u", *addr);
108101
return (result);
109102
}
110103

@@ -190,7 +183,7 @@ lo_manage(PG_FUNCTION_ARGS)
190183

191184
if ((orig != newv && (orig == NULL || newv == NULL)) || (orig != NULL && newv != NULL && strcmp(orig, newv)))
192185
DirectFunctionCall1(lo_unlink,
193-
ObjectIdGetDatum((Oid) atoi(orig)));
186+
ObjectIdGetDatum(atooid(orig)));
194187

195188
if (newv)
196189
pfree(newv);
@@ -211,7 +204,7 @@ lo_manage(PG_FUNCTION_ARGS)
211204
if (orig != NULL)
212205
{
213206
DirectFunctionCall1(lo_unlink,
214-
ObjectIdGetDatum((Oid) atoi(orig)));
207+
ObjectIdGetDatum(atooid(orig)));
215208

216209
pfree(orig);
217210
}

contrib/lo/lo.sql.in

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--
22
-- PostgreSQL code for LargeObjects
33
--
4-
-- $Id: lo.sql.in,v 1.5 2000/11/20 20:36:55 tgl Exp $
4+
-- $Id: lo.sql.in,v 1.6 2000/11/21 21:51:58 tgl Exp $
55
--
66
--
77
-- Create the data type
@@ -33,6 +33,15 @@ create function lo_oid(lo)
3333
as 'MODULE_PATHNAME'
3434
language 'c';
3535

36+
-- same function, named to allow it to be used as a type coercion, eg:
37+
-- create table a (image lo);
38+
-- select image::oid from a;
39+
--
40+
create function oid(lo)
41+
returns oid
42+
as 'MODULE_PATHNAME', 'lo_oid'
43+
language 'c';
44+
3645
-- this allows us to convert an oid to a managed lo object
3746
-- ie: insert into test values (lo_import('/fullpath/file')::lo);
3847
create function lo(oid)
@@ -44,13 +53,4 @@ create function lo(oid)
4453
create function lo_manage()
4554
returns opaque
4655
as 'MODULE_PATHNAME'
47-
language 'C';
48-
49-
-- This allows us to map lo to oid
50-
--
51-
-- eg:
52-
-- create table a (image lo);
53-
-- select image::oid from a;
54-
--
55-
create function oid(lo) returns oid as 'select lo_oid($1)' language 'sql';
56-
56+
language 'c';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy