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

Encoding an array of composite types fails with a segmentation fault #620

Closed
jacamera opened this issue Jan 5, 2025 · 3 comments · Fixed by #621
Closed

Encoding an array of composite types fails with a segmentation fault #620

jacamera opened this issue Jan 5, 2025 · 3 comments · Fixed by #621

Comments

@jacamera
Copy link

jacamera commented Jan 5, 2025

Environment:

  • Ubuntu 24.04.1 LTS
  • ruby 3.2.3
  • pg 1.5.9

Isolated Example:

require 'pg'

map = PG::TypeMapByColumn.new([PG::TextEncoder::Integer.new, PG::TextEncoder::String.new])
record_encoder = PG::TextEncoder::Record.new(type_map: map)
array_encoder = PG::TextEncoder::Array.new(elements_type: record_encoder)
array_encoder.encode([[123, 'Main St.']])

Log Output:
log.txt

larskanis added a commit that referenced this issue Jan 10, 2025
This could cause a segfault.

Fixes #620
larskanis added a commit that referenced this issue Jan 10, 2025
Setting dimensions is especially useful, when a Record shall be encoded into an Array, since the Array encoder can not distinguish if the array shall be encoded as a higher dimension or as a record otherwise.

Related to #620
larskanis added a commit that referenced this issue Jan 10, 2025
Setting dimensions is especially useful, when a Record shall be encoded into an Array, since the Array encoder can not distinguish if the array shall be encoded as a higher dimension or as a record otherwise.

Related to #620
larskanis added a commit that referenced this issue Jan 10, 2025
This could cause a segfault.

Fixes #620
@jacamera
Copy link
Author

Thanks for fixing this so quickly!

@larskanis
Copy link
Collaborator

Your intention seems to be to encode a record into an array. This doesn't work so far, but I implemented #622 to make it possible. The array must be set to 1 dimension:

array_encoder = PG::TextEncoder::Array.new(elements_type: record_encoder, dimensions: 1)

@jacamera
Copy link
Author

Yup, that seemed like a sensible workaround. For my use case I wanted to build up a record encoder and decoder dynamically for a nested graph of composite types. The decoder worked but then when I tried to re-encode the output I ran into this issue.

My use case only allows for arrays of records or primitives, not arrays of arrays, so I can just specify dimensions: 1. Though I suppose you could always just keep a count of the dimensions as well and still be able to perform round-trip encoding and decoding with any number of nested arrays which is a nice feature.

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 a pull request may close this issue.

2 participants