Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using NpgsqlParameter with empty value throws ArgumentNullException #5446

Closed
kbogun opened this issue Nov 23, 2023 · 3 comments
Closed

Using NpgsqlParameter with empty value throws ArgumentNullException #5446

kbogun opened this issue Nov 23, 2023 · 3 comments
Assignees
Labels
Milestone

Comments

@kbogun
Copy link

kbogun commented Nov 23, 2023

Steps to reproduce

using (NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Database=test;Username=test_user;Password=test_pass"))
using (NpgsqlCommand command = connection.CreateCommand())
{
   connection.Open();
   
   command.CommandText = "CALL procedureName(foo=>@p_foo)";

   NpgsqlParameter param = new NpgsqlParameter();
   param.ParameterName = "@p_foo";
   param.Value = "";
   command.Parameters.Add(param);

   command.ExecuteNonQuery();
}

The issue

Using NpgsqlParameter with empty value throws ArgumentNullException. In version 7.0.6 it works correctly.

Exception message: System.ArgumentNullException: 'Array cannot be null. Parameter name: chars'
Stack trace:
   at System.Text.UTF8Encoding.GetByteCount(Char* chars, Int32 count)
   at System.Text.EncodingExtensions.GetByteCount(Encoding encoding, ReadOnlySpan`1 chars)
   at Npgsql.Internal.Converters.StringBasedTextConverter`1.GetSize(SizeContext context, T value, Object& writeState)
   at Npgsql.Internal.PgConverter`1.GetSizeAsObject(SizeContext context, Object value, Object& writeState)
   at Npgsql.Internal.PgConverterExtensions.GetSizeOrDbNullAsObject(PgConverter converter, DataFormat format, Size writeRequirement, Object value, Object& writeState)
   at Npgsql.Internal.PgTypeInfo.BindObject(PgConverter converter, Object value, Size& size, Object& writeState, DataFormat& format, Nullable`1 formatPreference)
   at Npgsql.NpgsqlParameter.BindCore(Boolean allowNullReference)
   at Npgsql.NpgsqlParameter.Bind(DataFormat& format, Size& size)
   at Npgsql.NpgsqlParameterCollection.ProcessParameters(PgSerializerOptions options, Boolean validateValues, CommandType commandType)
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__119.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__119.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlCommand.<ExecuteNonQuery>d__107.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()

Further technical details

Npgsql version: 8.0.0
PostgreSQL version: 16.0
Operating system: Windows 10 22H2

@xxl-cc
Copy link

xxl-cc commented Nov 24, 2023

I encountered the same problem using nlog to write logs to the database, which was normal in 7.0.6.

@ebitencourt
Copy link

same problem using QueryOver
.Where(d => d.proc == null || d.proc == "")

@NinoFloris
Copy link
Member

@kbogun thanks for your report.

I noticed in your stacktrace you're running on an older TFM for which we unfortunately don't have good CI coverage.

And indeed the cause of this issue is in the polyfills we're using on .NET Standard builds where we don't have System.Text.Encoding methods taking spans.

I've submitted a fix in #5453 which will be part of 8.0.1.

@NinoFloris NinoFloris added the bug label Nov 24, 2023
@NinoFloris NinoFloris self-assigned this Nov 24, 2023
@NinoFloris NinoFloris added this to the 8.0.1 milestone Nov 24, 2023
JonasWestman pushed a commit to monitor-erp/npgsql that referenced this issue Dec 20, 2023
Fixes npgsql#5446

Signed-off-by: monjowe <jonas.westman@monitor.se>
JonasWestman pushed a commit to monitor-erp/npgsql that referenced this issue Dec 21, 2023
Fixes npgsql#5446

Signed-off-by: monjowe <jonas.westman@monitor.se>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants