question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reading bit value goes into an infinity loop

See original GitHub issue

When reading a bit type value, I encountered a bug that caused an infinite loop under certain conditions.

Steps to reproduce

using (var conn = new NpgsqlConnection(connnectionString))
{
    conn.Open();

    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = @"SELECT 1::bit(100000);";

        using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            reader.Read();
            reader.GetValue(0); // infinite loop!!
        }
    }
}

The issue

BitStringHandler has the cause. In case a bit value cuts across a buffer boundary, the last byte will never read and result in an infinite loop. The buf.ReadBytesLeft property remains at 1 and the loop cannot be escaped.

https://github.com/npgsql/npgsql/blob/b1fd3f2add28dbcff554eb117c83189d91f8c183/src/Npgsql/TypeHandlers/BitStringHandler.cs#L58-L74

Above example is terrible, but our application is facing same problem. It reads 16-bit column values, and cuts across a buffer boundary depending on the data state.

If I set CommandBehavior to default, it works. but we use Dapper, and Dapper’s Query method internally sets CommandBehavior.SequentialAccess link.

Further technical details

Npgsql version: 4.1.2 PostgreSQL version: 9.6 Operating system: Windows 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
YohDeadfallcommented, Dec 14, 2019

Would like, but without a rush. Am out of the city till tomorrow (:

0reactions
mintsoftcommented, Jan 25, 2020

@YohDeadfall Aha that makes sense, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

my loop goes to an endless loop. How can I solve it? ...
The code successfully parses 123 into an integer, and assigns it to input . No infinite loop, the program has completed. System.in: scan:...
Read more >
What is an infinite loop (endless loop)?
An infinite loop is piece of coding that repeats itself indefinitely. Learn how it works and when it can be useful or harmful...
Read more >
Infinite loop when entering a character? - C++ Forum
Is that a random memory address? I went back to the loop I made and entered the value -858993460 when asked to enter...
Read more >
Best way to track down infinite loop?
I've gotten MonoDevelop to stop infinite loops. Just switch to MD, start debugging, and press "Pause". It'll stop you somewhere in the infinite ......
Read more >
How to Find the Exact Line of an Infinite Loop Bug in the code
It's a bit tricky to find and you might spend some time to know why it's happening but the good thing is you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found