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

Ensure valid PG::Result data pointer #42

Merged
merged 1 commit into from
Oct 4, 2019
Merged

Conversation

larskanis
Copy link
Collaborator

Allocation of the ruby typed data object VALUE is now done within the function that assigns the PGresult retrieved from libpq. Therefore the data pointer of the PG::Result instance is always valid.
There's no need to check the 'this' pointer at every use any longer.

These 'this' pointer checks were introduced at commit de2487c, when we moved to a variable size data memory. This variable memory allows us to store the field_names once per PG::Result and reuse them several times. However this also moved the memory allocation out of PG::Result#allocate, so that we had to check for validity of the pointer.

This patch removes PG::Result.allocate and PG::Result.new, which is obvoiusly an API change. However although these methods were callable, the resulting object wasn't usable at all. Not even PG::Result#inspect did work. I therefore beleave that no one out there calls these methods and that we are safe to remove them within the pg-1.x series.

This also changes the base class of PG::Result to rb_cData as recommended in Ruby MRI. This change undefs the alloc function internally. It is also visible in ruby space: PG::Result.ancestors changes from
[PG::Result, PG::Constants, Enumerable, Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject] to
[PG::Result, PG::Constants, Enumerable, Data, Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject] .
I also think that this is no change that legitimate a jump to pg-2.0.

@ged, @cbandy Is it OK for you to merge this?

Allocation of the ruby typed data object VALUE is now done within the function that assigns the PGresult retrieved from libpq.
Therefore the data pointer of the PG::Result instance is always valid.
There's no need to check the 'this' pointer at every use any longer.

These 'this' pointer checks were introduced at commit de2487c, when we moved to a variable size data memory.
This variable memory allows us to store the field_names once per PG::Result and reuse them several times.
However this also moved the memory allocation out of PG::Result#allocate, so that we had to check for validity of the pointer.

This patch removes PG::Result.allocate and PG::Result.new , which is obvoiusly an API change.
However although these methods were callable, the resulting object wasn't usable at all.
Not even PG::Result#inspect did work.
I therefore beleave that no one out there calls these methods and that we are safe to remove them within the pg-1.x series.

This also changes the base class of PG::Result to rb_cData as recommended in Ruby MRI:
https://github.com/ruby/ruby/blob/c3dd3b95538a641bbffb02993985ce0cbac1b9d6/doc/extension.rdoc#user-content-c-struct-to-ruby-object
This change undefs the alloc function internally.
It is also visible in ruby space: PG::Result.ancestors changes from
[PG::Result, PG::Constants, Enumerable, Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject] to
[PG::Result, PG::Constants, Enumerable, Data, Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject] .
I also think that this is no change that legitimate a jump to pg-2.0.
Copy link
Owner

@ged ged left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me. I agree that I don't think this necessitates a major version bump; neither new nor allocate were part of the public API.

@larskanis larskanis merged commit 4fe03c9 into ged:master Oct 4, 2019
@larskanis
Copy link
Collaborator Author

Thank you Michael for reviewing!

@larskanis larskanis deleted the safe-result branch August 17, 2021 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants