Skip to content

library panics when trying to parse a untis time of 0 as chrono #13

@Rajala1404

Description

@Rajala1404

This function panics if the value (string) is 0

fn chrono_from_untis_time(value: u16) -> NaiveTime {
    let string = value.to_string();
    let hour_len = if string.len() == 3 { 1 } else { 2 };
    let hours = string[0..hour_len].parse::<u32>().unwrap(); // panics here when doing [0..hour_len]
    let mins = string[hour_len..(hour_len + 2)].parse::<u32>().unwrap();
    NaiveTime::from_hms_opt(hours, mins, 0).unwrap()
}

I modified the function to print the string and the hour_len value.

fn chrono_from_untis_time(value: u16) -> NaiveTime {
    let string = value.to_string();
    let hour_len = if string.len() == 3 { 1 } else { 2 };
    println!("String is: {}, hours_len: {}", string, hour_len);
    let hours = string[0..hour_len].parse::<u32>().unwrap();
    let mins = string[hour_len..(hour_len + 2)].parse::<u32>().unwrap();
    NaiveTime::from_hms_opt(hours, mins, 0).unwrap()
}

This is my function:

println!("timetable magic");
let timetable = tokio::task::spawn_blocking(move || {
    client.own_timetable_for_week(&week.into()).map_err(|e| Error::UntisAPI(format!("failed to get timetable: '{}'", e)))
}).await.map_err(|e| Error::Threading(format!("Failed to join handle: '{}'", e)))??;

And here is the output:

timetable magic
String is: 815, hours_len: 1
String is: 2359, hours_len: 2
String is: 0, hours_len: 2

called `Result::unwrap()` on an `Err` value: Threading("Failed to join handle: \"task 27 panicked with message \"byte index 2 is out of bounds of `0`\"\"")
thread 'tokio-runtime-worker' panicked at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/datetime.rs:136:23:
byte index 2 is out of bounds of `0`
stack backtrace
stack backtrace:
   0: rust_begin_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
   2: core::str::slice_error_fail_rt
   3: core::str::slice_error_fail
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/str/mod.rs:89:5
   4: core::str::traits::<impl core::slice::index::SliceIndex<str> for core::ops::range::Range<usize>>::index
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/str/traits.rs:245:21
   5: <alloc::string::String as core::ops::index::Index<I>>::index
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/string.rs:2466:9
   6: untis::datetime::chrono_from_untis_time
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/datetime.rs:136:23
   7: <untis::datetime::Time as serde::de::Deserialize>::deserialize
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/datetime.rs:120:17
   8: <core::marker::PhantomData<T> as serde::de::DeserializeSeed>::deserialize
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:800:9
   9: <serde::de::value::MapDeserializer<I,E> as serde::de::MapAccess>::next_value_seed
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/value.rs:1383:9
  10: serde::de::MapAccess::next_value
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1873:9
  11: <&mut A as serde::de::MapAccess>::next_value
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1954:9
  12: <untis::resources::_::<impl serde::de::Deserialize for untis::resources::Lesson>::deserialize::__Visitor as serde::de::Visitor>::visit_map
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/resources.rs:278:73
  13: serde::__private::de::content::visit_content_map_ref
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:1728:26
  14: <serde::__private::de::content::ContentRefDeserializer<E> as serde::de::Deserializer>::deserialize_struct
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:2021:40
  15: untis::resources::_::<impl serde::de::Deserialize for untis::resources::Lesson>::deserialize
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/resources.rs:278:73
  16: <core::marker::PhantomData<T> as serde::de::DeserializeSeed>::deserialize
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:800:9
  17: <serde::de::value::SeqDeserializer<I,E> as serde::de::SeqAccess>::next_element_seed
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/value.rs:1096:17
  18: serde::de::SeqAccess::next_element
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1734:9
  19: <&mut A as serde::de::SeqAccess>::next_element
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1763:9
  20: <serde::de::impls::<impl serde::de::Deserialize for alloc::vec::Vec<T>>::deserialize::VecVisitor<T> as serde::de::Visitor>::visit_seq
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/impls.rs:1175:46
  21: serde::__private::de::content::visit_content_seq_ref
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:1707:26
  22: <serde::__private::de::content::ContentRefDeserializer<E> as serde::de::Deserializer>::deserialize_seq
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:1976:40
  23: serde::de::impls::<impl serde::de::Deserialize for alloc::vec::Vec<T>>::deserialize
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/impls.rs:1186:9
  24: <core::marker::PhantomData<T> as serde::de::DeserializeSeed>::deserialize
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:800:9
  25: <serde::de::value::MapDeserializer<I,E> as serde::de::MapAccess>::next_value_seed
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/value.rs:1383:9
  26: serde::de::MapAccess::next_value
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1873:9
  27: <&mut A as serde::de::MapAccess>::next_value
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/de/mod.rs:1954:9
  28: <untis::jsonrpc::_::<impl serde::de::Deserialize for untis::jsonrpc::Response<T>>::deserialize::__Visitor<T> as serde::de::Visitor>::visit_map
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/jsonrpc.rs:41:50
  29: serde::__private::de::content::visit_content_map_ref
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:1728:26
  30: <serde::__private::de::content::ContentRefDeserializer<E> as serde::de::Deserializer>::deserialize_any
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.217/src/private/de.rs:1769:40
  31: untis::jsonrpc::_::<impl serde::de::Deserialize for untis::jsonrpc::Response<T>>::deserialize
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/jsonrpc.rs:41:50
  32: serde_json::de::from_trait
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.135/src/de.rs:2495:22
  33: serde_json::de::from_str
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.135/src/de.rs:2696:5
  34: untis::jsonrpc::Client::request
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/jsonrpc.rs:102:37
  35: untis::client::Client::timetable_between
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/client.rs:189:9
  36: untis::client::Client::own_timetable_between
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/client.rs:122:9
  37: untis::client::Client::own_timetable_for_week
             at /home/rajala1404/.cargo/git/checkouts/untis-rs-aada97f77ee49258/2b3569b/src/client.rs:113:9
  38: lanis_rs::modules::timetable::Week::new::{{closure}}::untis::{{closure}}::{{closure}}
             at ./src/modules/timetable.rs:292:17
  39: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/task.rs:42:21
  40: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:331:17
  41: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/loom/std/unsafe_cell.rs:16:9
  42: tokio::runtime::task::core::Core<T,S>::poll
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:320:13
  43: tokio::runtime::task::harness::poll_future::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:532:19
  44: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panic/unwind_safe.rs:272:9
  45: std::panicking::try::do_call
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:557:40
  46: __rust_try
  47: std::panicking::try
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:521:19
  48: std::panic::catch_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panic.rs:350:14
  49: tokio::runtime::task::harness::poll_future
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:520:18
  50: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:209:27
  51: tokio::runtime::task::harness::Harness<T,S>::poll
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:154:15
  52: tokio::runtime::task::raw::poll
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:271:5
  53: tokio::runtime::task::raw::RawTask::poll
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:201:18
  54: tokio::runtime::task::UnownedTask<S>::run
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/mod.rs:486:9
  55: tokio::runtime::blocking::pool::Task::run
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:161:9
  56: tokio::runtime::blocking::pool::Inner::run
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:511:17
  57: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:469:13
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread 'tests::test_timetable' panicked at src/lib.rs:242:206:
called `Result::unwrap()` on an `Err` value: Threading("Failed to join handle: 'task 25 panicked with message \"byte index 2 is out of bounds of `0`\"'")
stack backtrace:
   0: rust_begin_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
   2: core::result::unwrap_failed
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/result.rs:1679:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/result.rs:1102:23
   4: lanis_rs::tests::test_timetable::{{closure}}
             at ./src/lib.rs:242:35
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/future/future.rs:123:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/future/future.rs:123:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:729:57
   8: tokio::runtime::coop::with_budget
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:729:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:428:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:728:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:807:68
  14: tokio::runtime::context::scoped::Scoped<T>::set
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context/scoped.rs:40:9
  15: tokio::runtime::context::set_scheduler::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context.rs:180:26
  16: std::thread::local::LocalKey<T>::try_with
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/local.rs:283:12
  17: std::thread::local::LocalKey<T>::with
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/thread/local.rs:260:9
  18: tokio::runtime::context::set_scheduler
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context.rs:180:9
  19: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:807:27
  20: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:716:19
  21: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:196:28
  22: tokio::runtime::context::runtime::enter_runtime
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context/runtime.rs:65:16
  23: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/current_thread/mod.rs:184:9
  24: tokio::runtime::runtime::Runtime::block_on_inner
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/runtime.rs:368:47
  25: tokio::runtime::runtime::Runtime::block_on
             at /home/rajala1404/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/runtime.rs:342:13
  26: lanis_rs::tests::test_timetable
             at ./src/lib.rs:212:9
  27: lanis_rs::tests::test_timetable::{{closure}}
             at ./src/lib.rs:209:30
  28: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
  29: core::ops::function::FnOnce::call_once
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions