Re: lo_read error ??? - Mailing list pgsql-interfaces

From Douglas Thomson
Subject Re: lo_read error ???
Date
Msg-id 37C52FC0.3F33DAED@mugc.cc.monash.edu.au
Whole thread Raw
In response to lo_read error ???  (root <andrea@ssto-dtcb.interbusiness.it>)
List pgsql-interfaces
In a message that I found in the archives:
    http://www.PostgreSQL.ORG/mhonarc/pgsql-interfaces/1999-05/msg00230.html
Andrea Partinico writes:
> Hi to all,
>
> I'm trying to use large objects this way:
>
> -----------------------
> #!/usr/bin/perl
> $ENV{'PGDATESTYLE'} = "European";
> use CGI;
> use Pg;
> $base = new CGI;
> $fax = $base->url_param('fax');
> $page = $base->url_param('page');
> $conn = Pg::connectdb("dbname=www host=localhost");
> $select = $conn->exec("SELECT faxpage FROM faxric where codfax='$fax'");
>
> $faxpage = $select->fetchrow();
> $faxpage_fd = $conn->lo_open($faxpage, PGRES_INV_READ);
> if ($faxpage_fd < 0) {
>  print $base->header('text/plain'),
>  "Errore aprendo file su db";
>  exit;
> }
> print $base->header(-type=>'image/gif');
> while(($ret = $conn->lo_read($faxpage_fd, $buf, 1024)) >0 ) {
>  print "$buf";
> }
> $ret = $conn->lo_close($faxpage_fd);
>
> ------------------------
>
> and I get only an icon, but not the image
> so I modified the script ...
>
> ------------------------
> #!/usr/bin/perl
> $ENV{'PGDATESTYLE'} = "European";
> use CGI;
> use Pg;
> $base = new CGI;
> $fax = $base->url_param('fax');
> $page = $base->url_param('page');
> $conn = Pg::connectdb("dbname=www host=localhost");
> $select = $conn->exec("SELECT faxpage FROM faxric where codfax='$fax'");
>
> $faxpage = $select->fetchrow();
> $faxpage_fd = $conn->lo_open($faxpage, PGRES_INV_READ);
> if ($faxpage_fd < 0) {
>  print $base->header('text/plain'),
>  "Errore aprendo file su db";
>  exit;
> }
> print $base->header(-type=>'text/plain');
> while(($ret = $conn->lo_read($faxpage_fd, $buf, 1024)) >0 ) {
>  $test = length $buf;
>  print "$ret -- $test\n";
> }
> $ret = $conn->lo_close($faxpage_fd);
> ------------------------
>
> and I get this output:
>
> 1024 -- 11
> 1024 -- 179
> 1024 -- 172
> 1024 -- 187
> 1024 -- 120
> 1024 -- 232
> 1024 -- 182
> 1024 -- 98
> 1024 -- 15
> 1024 -- 129
> 1024 -- 241
> 1024 -- 315
> 1024 -- 221
> 1024 -- 1002
> 1024 -- 13
> 190 -- 110
>
> ------------------------
> why $buf do not contain all characters that lo_read() return ???
>
> system is Linux RedHat 6.0, whit Postgres 6.4.2
> (I've also tryed to compile it myself, but nothing change)
>
> large objects are gif images
> ------------------------
>
> Many tanks in advance to all.
> Andrea Partinico
> andrea@ssto-dtcb.interbusiness.it

The explanation is probably fairly simple: the BLOB is getting
truncated at the first NUL character it contains! The reason for
this is that lo_read in Pg.xs is using the default:
    OUTPUT:
        RETVAL
        buf
which uses C's strlen() to work out the length of the scalar.

The code ought to read something more like:
    OUTPUT:
        RETVAL
        buf sv_setpvn((SV*)ST(2), buf, RETVAL);

I am not sure if this needs to be done on both lo_read methods
in this file, but I changed both and have not since had any
problems with truncated BLOBs.

I will attach the (trivial) patch file in case that is useful to
anyone.

Let us know if this fixes your problem...

Douglas Thomson
Lecturer in Computing
Monash University, Gippsland Campus, AUSTRALIA

P.S. Several months ago I reported this problem, along with my
     suggested patch, to E.Mergl@bawue.de (who as far as I know
     is the code maintainer) but I received no response (neither
     e-mail nor any sign of the bug getting fixed) so I suspect
     my mail did not get through... if anyone can call this
     patch to the attention of anyone who can fix the original
     I would greatly appreciate it!diff -Naur perl5/Pg.xs perl5.fixed/Pg.xs
--- perl5/Pg.xs    Sat Aug 14 12:46:35 1999
+++ perl5.fixed/Pg.xs    Thu Aug 26 21:35:33 1999
@@ -1,6 +1,6 @@
 /*-------------------------------------------------------
  *
- * $Id: Pg.xs,v 1.12 1999/02/19 23:27:17 tgl Exp $
+ * $Id: Pg.xs,v 1.12 1999/02/19 23:27:17 tgl Exp $ with patch for NULs
  *
  * Copyright (c) 1997, 1998  Edmund Mergl
  *
@@ -643,7 +643,7 @@
         }
     OUTPUT:
         RETVAL
-        buf
+        buf sv_setpvn((SV*)ST(2), buf, RETVAL);   /* to handle NULs */

 int
 lo_write(conn, fd, buf, len)
@@ -1029,7 +1029,7 @@
         }
     OUTPUT:
         RETVAL
-        buf
+        buf sv_setpvn((SV*)ST(2), buf, RETVAL);   /* to handle NULs */


 int

pgsql-interfaces by date:

Previous
From: Moray McConnachie
Date:
Subject: RE: [INTERFACES] Access 2000
Next
From: Marten Feldtmann
Date:
Subject: Problems with ODBC connect ...