pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nils Gösche <cartan@cartan.de>
To: pgsql-odbc@postgresql.org
Subject: Null Characters in Strings, Version 9.3.1
Date: Thu, 13 Feb 2014 18:53:20 +0100
Message-ID: <006a01cf28e4$7c425450$74c6fcf0$@de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>

Hi!

I have noticed a change of behavior in version 9.3.1 of the ODBC driver. I am using the 32 bit version for testing, haven’t tried 64 bit yet.

I am retrieving a string value from a table that has a column of type ‘text’. This worked just fine with version 9.2.1 of the driver, but since I upgraded the ODBC driver to 9.3.1, something strange is happening:

In psql, I do:

select length(body) from macros where name = 'check_pruefziffer';

and it returns 459. The text contains only ASCII characters.

If I retrieve the value in a C# program using the ODBC driver, I get a string that has a null character at position 459, but a total length of 487!  The string up to position 458 is correct, but has now been extended with a null character and a few junk characters.

This shouldn’t be happening, right? Am I doing something wrong here? Here is some C# code that I wrote for testing:

    class Program
    {
        const string ConnString = @"Driver={PostgreSQL Unicode};server=localhost;port=5432;database=...;uid...;pwd=...";

        static void Main(string[] args)
        {
            try
            {
                using (var db = new OdbcConnection(ConnString))
                {
                    db.Open();
                    using (var trans = db.BeginTransaction())
                    {
                        try
                        {
                            const string cmdText = @"SELECT body FROM macros WHERE name = 'check_pruefziffer'";
                            using (var cmd = new OdbcCommand(cmdText, db, trans))
                            {
                                var ret = cmd.ExecuteScalar();
                                if (ret == null)
                                {
                                    Console.WriteLine("ret == null");
                                }
                                else
                                {
                                    Console.WriteLine("ret is {0}: {1}", ret.GetType(), ret);
                                    Console.WriteLine("\nChecking for null byte...");
                                    string s = (string) ret;
                                    int pos = s.IndexOf('\0');
                                    if (pos >= 0)
                                    {
                                        Console.WriteLine("Found null byte at {0}!  Total = {1}", pos, s.Length);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Not found.");
                                    }
                                }
                            }
                            trans.Commit();
                        }
                        catch
                        {
                            trans.Rollback();
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message);
            }
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }
    }

Here is the tail of the output:

[SNIP]

  $computedZiffer = 10 - ($sum % 10)

  if ($computedZiffer -eq $pruefZiffer) { $ret = $true }
}
$ret ) { $ret = $true }
}
$ret

Checking for null byte...
Found null byte at 459!  Total = 487
Press any key to exit...

If you select the same value in psql, the tail looks like this:

[SNIP]
                                                                +
   $computedZiffer = 10 - ($sum % 10)                           +
                                                                +
   if ($computedZiffer -eq $pruefZiffer) { $ret = $true }       +
 }                                                              +
 $ret
(1 row)

The string that the C# program is getting contains a null byte plus some junk after the $ret.

Regards,
-- 
Nils Gösche
"Don't ask for whom the <CTRL-G> tolls."




-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



view thread (15+ messages)  latest in thread

Message-ID: <006a01cf28e4$7c425450$74c6fcf0$@de>
Permalink:  ../006a01cf28e4$7c425450$74c6fcf0$@de/
Also on:    postgresql.org/message-id/006a01cf28e4$7c425450$74c6fcf0$@de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: cartan@cartan.de
  Subject: Re: Null Characters in Strings, Version 9.3.1
  In-Reply-To: <006a01cf28e4$7c425450$74c6fcf0$@de>
  References: <006a01cf28e4$7c425450$74c6fcf0$@de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox