Null Characters in Strings, Version 9.3.1 - Mailing list pgsql-odbc

From Nils Gösche
Subject Null Characters in Strings, Version 9.3.1
Date
Msg-id 006a01cf28e4$7c425450$74c6fcf0$@de
Whole thread Raw
Responses Re: Null Characters in Strings, Version 9.3.1
List pgsql-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’ttried 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
ofthe 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
characterand 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."




pgsql-odbc by date:

Previous
From: Christoph Berg
Date:
Subject: 09.03.0100 cursor failures on various architectures
Next
From: Heikki Linnakangas
Date:
Subject: Re: 09.03.0100 cursor failures on various architectures