sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.51k stars 82 forks source link

Runtime exception: byte index 15 is not a char boundary #96

Closed qmdx closed 4 years ago

qmdx commented 4 years ago

Possible problems:

r2d2-0.8.7/src/lib.rs:554

use diesel::{
    pg::PgConnection,
    r2d2::{self, ConnectionManager},
};

embed_migrations!();

pub type Connection = PgConnection;
pub type Pool = r2d2::Pool<ConnectionManager<Connection>>;

pub fn migrate_and_config_db(url: &str) -> Pool {
    info!("Migrating and configurating database...");
    let manager = ConnectionManager::<Connection>::new(url);

    // The following line is the error point
    let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool.");

    embedded_migrations::run(&pool.get().expect("Failed to migrate."));

    pool
}

Error Log

thread 'main' panicked at 'byte index 15 is not a char boundary; it is inside '\u{5a7}' (bytes 13..15) of `��������:  ��֧�ֵ�ǰ��Э�� 1234.5679: �����֧�� 2.0 �� 3.0
`', src/libcore/str/mod.rs:2068:5
stack backtrace:
   0:        0x10409e4a5 - backtrace::backtrace::libunwind::trace::h5374dc6304209fb0
                               at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.37/src/backtrace/libunwind.rs:88
   1:        0x10409e4a5 - backtrace::backtrace::trace_unsynchronized::hc6c40d142cd2a86c
                               at /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.37/src/backtrace/mod.rs:66
   2:        0x10409e4a5 - std::sys_common::backtrace::_print_fmt::h070478e0aa6dc01e
                               at src/libstd/sys_common/backtrace.rs:76
   3:        0x10409e4a5 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h66f38d4abb2e41c1
                               at src/libstd/sys_common/backtrace.rs:60
   4:        0x1040be200 - core::fmt::write::h4f487e714088986d
                               at src/libcore/fmt/mod.rs:1030
   5:        0x1040997eb - std::io::Write::write_fmt::h7bd7b6fe8b47c5fb
                               at src/libstd/io/mod.rs:1412
   6:        0x1040a01ca - std::sys_common::backtrace::_print::hcf7e952e5633d242
                               at src/libstd/sys_common/backtrace.rs:64
   7:        0x1040a01ca - std::sys_common::backtrace::print::hbc3f189fef9884dc
                               at src/libstd/sys_common/backtrace.rs:49
   8:        0x1040a01ca - std::panicking::default_hook::{{closure}}::heee79636c241547c
                               at src/libstd/panicking.rs:196
   9:        0x10409fed5 - std::panicking::default_hook::hfcbd07059d15441e
                               at src/libstd/panicking.rs:210
  10:        0x1040a0927 - std::panicking::rust_panic_with_hook::h0c4b67125f55410a
                               at src/libstd/panicking.rs:473
  11:        0x1040a046d - std::panicking::continue_panic_fmt::h0e74ab2b215a1401
                               at src/libstd/panicking.rs:380
  12:        0x1040a0369 - rust_begin_unwind
                               at src/libstd/panicking.rs:307
  13:        0x1040ba75f - core::panicking::panic_fmt::h09741a3213dba543
                               at src/libcore/panicking.rs:85
  14:        0x1040bc188 - core::str::slice_error_fail::h52c839f600c0c2fb
                               at src/libcore/str/mod.rs:2068
  15:        0x1040bbed4 - core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::{{closure}}::h61f9c2d27d255fdd
                               at src/libcore/str/mod.rs:1792
  16:        0x1040bf5bb - core::option::Option<T>::unwrap_or_else::h3011a8fb78931a38
                               at src/libcore/option.rs:419
  17:        0x1040bf5bb - core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::h420d6546720206ab
                               at src/libcore/str/mod.rs:1792
  18:        0x1040bf5bb - core::str::traits::<impl core::ops::index::Index<I> for str>::index::h14d3ebe245eb52d4
                               at src/libcore/str/mod.rs:1657
  19:        0x1040bf5bb - <str as core::fmt::Debug>::fmt::h292cec7d55a079bb
                               at src/libcore/fmt/mod.rs:1978
  20:        0x1040643d2 - <alloc::string::String as core::fmt::Debug>::fmt::h17f9d64245995758
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/liballoc/string.rs:1888
  21:        0x104064340 - <&T as core::fmt::Debug>::fmt::h0ba60f86e2f59974
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/fmt/mod.rs:1928
  22:        0x1040bd858 - core::fmt::builders::DebugTuple::field::{{closure}}::h8ab1a10186f40902
                               at src/libcore/fmt/builders.rs:306
  23:        0x1040bd858 - core::result::Result<T,E>::and_then::hf5995138445c67fa
                               at src/libcore/result.rs:691
  24:        0x1040bd858 - core::fmt::builders::DebugTuple::field::h81d10e085ced9402
                               at src/libcore/fmt/builders.rs:293
  25:        0x1040641e4 - <core::option::Option<T> as core::fmt::Debug>::fmt::hf4a5773ad4109a6e
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/option.rs:148
  26:        0x104064120 - <&T as core::fmt::Debug>::fmt::h35e781b5477368b6
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/fmt/mod.rs:1928
  27:        0x1040bd858 - core::fmt::builders::DebugTuple::field::{{closure}}::h8ab1a10186f40902
                               at src/libcore/fmt/builders.rs:306
  28:        0x1040bd858 - core::result::Result<T,E>::and_then::hf5995138445c67fa
                               at src/libcore/result.rs:691
  29:        0x1040bd858 - core::fmt::builders::DebugTuple::field::h81d10e085ced9402
                               at src/libcore/fmt/builders.rs:293
  30:        0x104064306 - <r2d2::Error as core::fmt::Debug>::fmt::h13f80e3f75067f64
                               at /Users/jobob/.cargo/registry/src/github.com-1ecc6299db9ec823/r2d2-0.8.7/src/lib.rs:554
  31:        0x1040be200 - core::fmt::write::h4f487e714088986d
                               at src/libcore/fmt/mod.rs:1030
  32:        0x1040a0683 - core::fmt::Write::write_fmt::h8daee13061d9f1d5
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/fmt/mod.rs:195
  33:        0x1040a0683 - std::panicking::continue_panic_fmt::PanicPayload::fill::{{closure}}::h0d2c4bb0ea7ff1ac
                               at src/libstd/panicking.rs:355
  34:        0x1040a0683 - core::option::Option<T>::get_or_insert_with::h0b15a3588f77aac8
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/option.rs:841
  35:        0x1040a0683 - std::panicking::continue_panic_fmt::PanicPayload::fill::ha1000078e695b52c
                               at src/libstd/panicking.rs:353
  36:        0x1040a0683 - <std::panicking::continue_panic_fmt::PanicPayload as core::panic::BoxMeUp>::get::hd9bfe89a18c769fc
                               at src/libstd/panicking.rs:368
  37:        0x1040a090d - std::panicking::rust_panic_with_hook::h0c4b67125f55410a
                               at src/libstd/panicking.rs:472
  38:        0x1040a046d - std::panicking::continue_panic_fmt::h0e74ab2b215a1401
                               at src/libstd/panicking.rs:380
  39:        0x1040a0369 - rust_begin_unwind
                               at src/libstd/panicking.rs:307
  40:        0x1040ba75f - core::panicking::panic_fmt::h09741a3213dba543
                               at src/libcore/panicking.rs:85
  41:        0x1040ba839 - core::result::unwrap_failed::hf47c31c429b02014
                               at src/libcore/result.rs:1165
  42:        0x1037dc26d - core::result::Result<T,E>::expect::h3bdda1ce5047ac42
                               at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/result.rs:960
sfackler commented 4 years ago

It appears that diesel is putting invalid UTF-8 into a string. This issue should be filed with that project.

qmdx commented 4 years ago

thanks , The problem is found. It's really a Postgres instance of diesel reading instead of utf8

weiznich commented 4 years ago

To be clear: Diesel does not put any invalid UTF-8 string anywhere. I would guess that this is an issue with a not supported/broken setup.

sfackler commented 4 years ago

The invalid string would be in the diesel error type, not in the connection manager.

weiznich commented 4 years ago

The only potential point where this could happen is if the database returns a non utf8 string as error message. In this case the user has an unsupported setup from diesels point of view.

sfackler commented 4 years ago

If you are providing a safe interface, you are obligated to ensure that your interface does not expose safe code to undefined behavior. Building a string from invalid UTF-8 data is undefined behavior.

sfackler commented 4 years ago

https://github.com/diesel-rs/diesel/blob/77afd4d09421b9db06665b831a7770eec7fd4425/diesel/src/pg/connection/raw.rs#L109

weiznich commented 4 years ago

To be sure: The underlying encoding is set to UTF8, so if I'm sure that's not the problem. Edit: Your are right, there is one way this is called before the encoding is set. That definitely should be fixed, because that's a bug. Nerveless: This will continue to be an unsupported configuration, so the fix will "just" improve the error message.

weiznich commented 4 years ago

I've put a fix into https://github.com/diesel-rs/diesel/pull/2239

qmdx commented 4 years ago

@weiznich @sfackler

This anomaly can be reproduced, When Postgres database Collation Order And Character Classification is not inutf8 format.