ctm / mb2-doc

Mb2, poker software
https://devctm.com
7 stars 2 forks source link

Zalgo text errors? #1340

Closed ctm closed 4 months ago

ctm commented 4 months ago

See what happens when pasting zalgo text into the lobby.

ts4z: deadhead if you grab some zalgo test and paste it in the chat box, you can get some interesting errors. ts4z: (especially if you turn up the craziness level)

deadhead: Thanks. I'll give it a spin locally.

ctm commented 4 months ago

Maximizing the craziness and using the phrase "When in the course of human events it becomes self-evident that all men are created equal..." caused an assertion to fail:

300.mb2-web.js:1 panicked at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/string.rs:1396:13:
assertion failed: self.is_char_boundary(new_len)

That's not inside my code, and I don't know how to find where it really is. However, searching .cargo gives me:

% rg -g string.rs 'assert!\(self.is_char_boundary\(new_len\)\)'
src/index.crates.io-6f17d22bba15001f/bumpalo-3.14.0/src/collections/string.rs
1174:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/bumpalo-3.10.0/src/collections/string.rs
1156:            assert!(self.is_char_boundary(new_len));

src/index.crates.io-6f17d22bba15001f/bumpalo-3.13.0/src/collections/string.rs
1174:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/heapless-0.7.15/src/string.rs
221:            assert!(self.is_char_boundary(new_len));

src/index.crates.io-6f17d22bba15001f/bumpalo-3.15.3/src/collections/string.rs
1185:            assert!(self.is_char_boundary(new_len));

src/index.crates.io-6f17d22bba15001f/bumpalo-3.15.4/src/collections/string.rs
1185:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/bumpalo-3.7.0/src/collections/string.rs
1189:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/bumpalo-3.12.2/src/collections/string.rs
1174:            assert!(self.is_char_boundary(new_len));

src/index.crates.io-6f17d22bba15001f/heapless-0.7.17/src/string.rs
221:            assert!(self.is_char_boundary(new_len));

src/index.crates.io-6f17d22bba15001f/heapless-0.7.16/src/string.rs
221:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/bumpalo-3.12.0/src/collections/string.rs
1174:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/bumpalo-3.8.0/src/collections/string.rs
1189:            assert!(self.is_char_boundary(new_len));

src/github.com-1ecc6299db9ec823/heapless-0.7.16/src/string.rs
221:            assert!(self.is_char_boundary(new_len));

and registry/src/github.com-1ecc6299db9ec823/bumpalo-3.8.0/src/collections/string.rs contains:

...
    /// # Panics
    ///
    /// Panics if `new_len` does not lie on a [`char`] boundary.
    ///
    /// [`char`]: https://doc.rust-lang.org/nightly/std/primitive.char.html
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use bumpalo::{Bump, collections::String};
    ///
    /// let b = Bump::new();
    ///
    /// let mut s = String::from_str_in("hello", &b);
    ///
    /// s.truncate(2);
    ///
    /// assert_eq!("he", s);
    /// ```
    #[inline]
    pub fn truncate(&mut self, new_len: usize) {
        if new_len <= self.len() {
            assert!(self.is_char_boundary(new_len));
            self.vec.truncate(new_len)
        }
    }

which caused me to look for truncate calls and indeed, I found some in chat:

rg '\.truncate\('
src/chat.rs
136:                    chat.truncate(120);

src/table.rs
724:                                chat.truncate(120);

src/lobby.rs
727:                        chat.truncate(255);

Those are bad and can indeed cause the panic we've seen. I'll replace them with something safe.

ctm commented 4 months ago

The solution is to add a char_truncate to String and then replace those three calls to truncate with char_truncate.

trait CharTruncate {
    fn char_truncate(&mut self, new_len: usize);
}

impl CharTruncate for String {
    fn char_truncate(&mut self, new_len: usize) {
        if let Some(new_len) = self
            .char_indices()
            .nth(new_len)
            .map(|(position, _char)| position)
        {
            self.truncate(new_len);
        }
    }
}

I've tested it and it works (duh). Deploying now.

ctm commented 4 months ago

FWIW, Docker hung, so I wound up using Activity Monitor to quit it, then I force quit the Docker backend and force quit the Docker network task, then restarte Docker and now Docker seems to be working fine. I'm doing development on my 2013 Macbook Pro while my 2018 Macbook Pro is in the shop.