Skip to content

Bug: command Clone() specifies incorrect parameter collection #6065

@mgravell

Description

@mgravell

Example:

NpgsqlCommand original = new NpgsqlCommand();
original.Parameters.Add(new NpgsqlParameter<int> { TypedValue = 42 });

// now play with it
var cmd = original;
var p = cmd.Parameters[0];
Console.WriteLine(ReferenceEquals(p.Collection, cmd.Parameters)); // True, great
Console.WriteLine(p.Collection!.Contains(p)); // True, great

// clone, and see again
cmd = original.Clone();
p = cmd.Parameters[0];
Console.WriteLine(ReferenceEquals(p.Collection, cmd.Parameters)); // False, uh...
Console.WriteLine(p.Collection!.Contains(p)); // False, uh-oh!
Console.WriteLine(ReferenceEquals(p.Collection, original.Parameters)); // True, well that explains it!

Impact: accessing anything via .Collection is unreliable, and depends on whether it is a clone of an earlier command.

Cause: CloneTo uses this instead of other here.

Proposed fix:

- newParam.Collection = this;
+ newParam.Collection = other;

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions