ArgumentError: string contains null byte

Issue #198 closed
Kazuhiro NISHIYAMA
created an issue

Rails 4.1.8 with pg gem 0.17.1 can create Memo.create(body: "foo\u0000bar"), but with pg gem 0.18.0 raises ArgumentError.

% bin/rails console --sandbox
Loading development environment in sandbox (Rails 4.1.8)
Any modifications you make will be rolled back on exit
irb(main):001:0> Memo.create(body: "foo\u0000bar")
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "memos" ("body", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["body", "foo\u0000bar"], ["created_at", "2015-01-05 06:08:28.319546"], ["updated_at", "2015-01-05 06:08:28.319546"]]
ArgumentError: string contains null byte: INSERT INTO "memos" ("body", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
   (0.2ms)  ROLLBACK TO SAVEPOINT active_record_1
ArgumentError: string contains null byte
    from /Users/knishiyama/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql_adapter.rb:832:in `send_query_prepared'
...

Comments (6)

  1. Lars Kanis

    This is a rails bug, that is fixed on the master branch and the fix will be part of rails-4.2.1: https://github.com/rails/rails/pull/17680

    When working with BYTEA data types, you can currently use rails from the git repository or keep using pg-0.17.1. However be aware that pg-0.17.1 is not fully compatible to Ruby-2.2.0 ! If you need this fix in rails-4.1, you should request backporting to the 4.1 branch at the rails team.

  2. vpereirabr

    hm,

    I have the same problem with rails 5 and pg 0.19.. using ruby 2.3.0

    2.3.3 :015 > n.content
    => "foo\u0000bar"
    2.3.3 :016 > n.save!
    (0.5ms) BEGIN
    SQL (0.5ms) INSERT INTO "package_files" ("path", "content", "package_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["path", "/foo/bar.sh"], ["content", "foo\u0000bar"], ["package_id", 2], ["created_at", 2017-01-05 21:01:49 UTC], ["updated_at", 2017-01-05 21:01:49 UTC]]
    (0.5ms) ROLLBACK
    ArgumentError: string contains null byte
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in async_exec' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:inblock in exec_no_cache'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in block in log' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:ininstrument'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in log' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:inexec_no_cache'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:587:in execute_and_clear' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:103:inexec_query'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in exec_insert' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:124:ininsert'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:17:in insert' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/relation.rb:65:ininsert'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/persistence.rb:560:in _create_record' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/counter_cache.rb:128:in_create_record'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/locking/optimistic.rb:75:in _create_record' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/attribute_methods/dirty.rb:123:in_create_record'
    ... 26 levels...
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in start' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:instart'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in console' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:inrun_command!'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/commands.rb:18:in <top (required)>' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/app_loader.rb:46:inrequire'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/app_loader.rb:46:in block in exec_app' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/app_loader.rb:35:inloop'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/app_loader.rb:35:in exec_app' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/lib/rails/cli.rb:5:in<top (required)>'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/exe/rails:9:in require' from /home/vpereira/.rvm/gems/ruby-2.3.3/gems/railties-5.0.1/exe/rails:9:in<top (required)>'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/bin/rails:22:in load' from /home/vpereira/.rvm/gems/ruby-2.3.3/bin/rails:22:in<main>'
    from /home/vpereira/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in eval' from /home/vpereira/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in<main>'
    2.3.3 :017 >

  3. Log in to comment