Skip to content

Commit

Permalink
Merge pull request #472 from chewing/0.7.0-rc.1-fix
Browse files Browse the repository at this point in the history
Bug fixes for 0.7.0-rc.1
  • Loading branch information
kanru authored Mar 31, 2024
2 parents b02ed5b + 3721ada commit 5fc999c
Show file tree
Hide file tree
Showing 23 changed files with 903 additions and 97 deletions.
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.21.0)
project(libchewing LANGUAGES C)

set(CMAKE_PROJECT_VERSION 0.7.0-rc.1)
set(CMAKE_PROJECT_VERSION 0.7.0-rc.2)
set(LIBCHEWING_VERSION ${CMAKE_PROJECT_VERSION})
set(PACKAGE_VERSION ${CMAKE_PROJECT_VERSION})
set(LIBCHEWING_BINARY_VERSION 1.0.0)
Expand Down Expand Up @@ -91,14 +91,13 @@ if(WITH_RUST)
FetchContent_MakeAvailable(Corrosion)

corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing CRATE_TYPES staticlib FEATURES capi)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing-testhelper)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_testhelper)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing-cli)
add_compile_definitions(WITH_RUST)
if(WITH_SQLITE3)
corrosion_set_features(chewing FEATURES sqlite)
corrosion_set_features(chewing-testhelper FEATURES sqlite)
corrosion_set_features(chewing_testhelper FEATURES sqlite)
endif()
corrosion_add_target_local_rustflags(chewing -Ccodegen-units=1)
if(ENABLE_GCOV)
corrosion_set_env_vars(chewing CARGO_INCREMENTAL=0)
corrosion_add_target_local_rustflags(chewing -Cinstrument-coverage -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort)
Expand Down Expand Up @@ -337,7 +336,7 @@ install(TARGETS libchewing DESTINATION ${CMAKE_INSTALL_LIBDIR})


set(CPACK_PACKAGE_CHECKSUM SHA256)
set(CPACK_SOURCE_IGNORE_FILES "/out" "/\\\\..*")
set(CPACK_SOURCE_GENERATOR TXZ)
set(CPACK_SOURCE_IGNORE_FILES "/out" "/target" "/\\\\..*")
set(CPACK_SOURCE_GENERATOR TZST)
set(CPACK_SOURCE_PACKAGE_FILE_NAME libchewing-${CMAKE_PROJECT_VERSION})
include(CPack)
include(CPack)
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "chewing"
description = "The Chewing (酷音) intelligent Zhuyin input method."
license = "LGPL-2.1-or-later"
documentation = "https://docs.rs/chewing"
version = "0.7.0-beta.1"
version = "0.7.0-beta.2"
rust-version = "1.70"
edition = "2021"

Expand Down
6 changes: 5 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
What's New in libchewing 0.7.0-rc.1 (Mar 25, 2024)
What's New in libchewing 0.7.0-rc.2 (Mar 31, 2024)
---------------------------------------------------------

This is the first beta release of the Rust rewrite. The library has been tested
Expand Down Expand Up @@ -36,6 +36,10 @@ and loading multiple dictionaries in future releases.
- Skip symbol selection if preedit buffer is empty (rust)
- Allow numlock in selection mode (rust)
- Avoid infinite recursion (rust)
- Ensure to return KeyBehavior::Commit when we push to commit buffer
- Adjust selection offset after delete symbols
- Disable trace logging if no logger is enabled
- Should not start selection in English mode

* Documentation
- Added document for fuzzers (rust)
Expand Down
2 changes: 1 addition & 1 deletion fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
afl = "0.15.4"
chewing = { version = "0.7.0-beta.1", path = "..", features = ["capi"] }
chewing = { version = "0.7.0-beta.2", path = "..", features = ["capi"] }
log = "0.4.21"
env_logger = "0.10.0"
xflags = "0.3.2"
6 changes: 6 additions & 0 deletions fuzzer/src/bin/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{ffi::CString, io::Read, ptr::null_mut};

use chewing::capi::{
input::*,
output::{chewing_buffer_Len, chewing_commit_Check},
setup::{chewing_delete, chewing_new2},
};

Expand Down Expand Up @@ -34,6 +35,7 @@ enum ChewingHandle {

impl From<u8> for ChewingHandle {
fn from(value: u8) -> Self {
let value = value % 23;
match value {
0 => Self::Default,
1 => Self::Backspace,
Expand Down Expand Up @@ -85,6 +87,7 @@ pub fn main() {
while let Some(Ok(op)) = ops.next() {
use ChewingHandle::*;

let buf_len = chewing_buffer_Len(ctx);
match ChewingHandle::from(op) {
Default => {
if let Some(Ok(key)) = ops.next() {
Expand Down Expand Up @@ -170,6 +173,9 @@ pub fn main() {
}
Skip => (),
}
if chewing_commit_Check(ctx) != 1 {
assert!(buf_len.abs_diff(chewing_buffer_Len(ctx)) < 2);
}
}
}
});
Expand Down
21 changes: 0 additions & 21 deletions src/capi/ffi.rs

This file was deleted.

14 changes: 12 additions & 2 deletions src/capi/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ impl ChewingLogger {
}

impl Log for ChewingLogger {
fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
true
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
if let Ok(logger) = self.logger.lock() {
if logger.is_some() && metadata.level() <= Level::Debug {
return true;
}
}
if let Ok(logger) = self.env_logger.lock() {
if let Some(el) = logger.as_ref() {
return el.enabled(metadata);
}
}
false
}

fn log(&self, record: &Record<'_>) {
Expand Down
1 change: 0 additions & 1 deletion src/capi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! Functions are organized into several modules according to the services
//! provided by them.
mod ffi;
mod io;
mod logger;
mod public;
Expand Down
24 changes: 16 additions & 8 deletions src/conversion/chewing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ops::Neg,
};

use log::{debug, trace, warn};
use log::{debug, log_enabled, trace, warn, Level::Trace};

use crate::dictionary::{Dictionary, Phrase};

Expand Down Expand Up @@ -150,9 +150,7 @@ impl ChewingEngine {
let len = selection.end - selection.start;
let substring: String =
phrase.as_str().chars().skip(offset).take(len).collect();
debug!("check {} against selection {}", substring, selection.phrase);
if substring != selection.phrase.as_ref() {
debug!("skip {}", substring);
continue 'next_phrase;
}
}
Expand Down Expand Up @@ -277,25 +275,35 @@ impl ChewingEngine {
fn trim_paths(&self, paths: Vec<PossiblePath>) -> Vec<PossiblePath> {
let mut trimmed_paths: Vec<PossiblePath> = vec![];
for candidate in paths.into_iter() {
trace!("Trim check {}", candidate);
if log_enabled!(Trace) {
trace!("Trim check {}", candidate);
}
let mut drop_candidate = false;
let mut keeper = vec![];
for p in trimmed_paths.into_iter() {
if drop_candidate || p.contains(&candidate) {
drop_candidate = true;
trace!(" Keep {}", p);
if log_enabled!(Trace) {
trace!(" Keep {}", p);
}
keeper.push(p);
continue;
}
if candidate.contains(&p) {
trace!(" Drop {}", p);
if log_enabled!(Trace) {
trace!(" Drop {}", p);
}
continue;
}
trace!(" Keep {}", p);
if log_enabled!(Trace) {
trace!(" Keep {}", p);
}
keeper.push(p);
}
if !drop_candidate {
trace!(" Keep {}", candidate);
if log_enabled!(Trace) {
trace!(" Keep {}", candidate);
}
keeper.push(candidate);
}
trimmed_paths = keeper;
Expand Down
6 changes: 3 additions & 3 deletions src/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ impl Composition {
if selection.start <= index {
if index < selection.end {
to_remove.push(i);
} else {
selection.start -= 1;
selection.end -= 1;
}
} else {
selection.start -= 1;
selection.end -= 1;
}
}
for i in to_remove {
Expand Down
10 changes: 0 additions & 10 deletions src/dictionary/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::{
cmp,
collections::{btree_map::Entry, BTreeMap, BTreeSet},
fmt::Debug,
io,
iter::{self, Empty},
path::Path,
str::{self, Utf8Error},
};

Expand All @@ -21,14 +19,6 @@ pub(crate) trait KVStore<'a> {
fn iter(&'a self) -> Self::KeyValueIter;
}

pub(crate) trait KVStoreBuilder {
fn create<P: AsRef<Path>>(filename: P) -> io::Result<Self>
where
Self: Sized;
fn add(&mut self, key: &[u8], data: &[u8]) -> io::Result<()>;
fn finish(self) -> io::Result<()>;
}

type PhraseKey = (Cow<'static, [u8]>, Cow<'static, str>);

pub(crate) struct KVDictionary<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ impl Error for BuildDictionaryError {
}
}

impl From<std::io::Error> for BuildDictionaryError {
fn from(source: std::io::Error) -> Self {
impl From<io::Error> for BuildDictionaryError {
fn from(source: io::Error) -> Self {
BuildDictionaryError {
source: Box::new(source),
}
Expand Down
4 changes: 2 additions & 2 deletions src/dictionary/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ impl From<RusqliteError> for BuildDictionaryError {
}
}

impl From<std::str::Utf8Error> for BuildDictionaryError {
fn from(source: std::str::Utf8Error) -> Self {
impl From<str::Utf8Error> for BuildDictionaryError {
fn from(source: str::Utf8Error) -> Self {
BuildDictionaryError {
source: Box::new(source),
}
Expand Down
34 changes: 19 additions & 15 deletions src/dictionary/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,28 @@ impl TrieDictionary {
let data = data_chunk
.ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "expecting data chunk"))?
.read_contents(&mut stream)?;
let crc32 = Crc32::new();
if dict.len() < size_of::<u32>() {
return Err(io::ErrorKind::InvalidData.into());
}
let dict_len = dict.len() - size_of::<u32>();
let crc = u32::from_le_bytes(dict[dict_len..].try_into().unwrap());
let check = crc32.check(&dict[..dict_len]);
if crc != check {
return Err(io::ErrorKind::InvalidData.into());
}
if data.len() < size_of::<u32>() {
return Err(io::ErrorKind::InvalidData.into());
}
let data_len = data.len().saturating_sub(size_of::<u32>());
let crc = u32::from_le_bytes(data[data_len..].try_into().unwrap());
let check = crc32.check(&data[..data_len]);
if crc != check {
return Err(io::ErrorKind::InvalidData.into());
// CRC checks slows down fuzzing considerably
#[cfg(not(fuzzing))]
{
let dict_len = dict.len() - size_of::<u32>();
let crc = u32::from_le_bytes(dict[dict_len..].try_into().unwrap());
let crc32 = Crc32::new();
let check = crc32.check(&dict[..dict_len]);
if crc != check {
return Err(io::ErrorKind::InvalidData.into());
}
if data.len() < size_of::<u32>() {
return Err(io::ErrorKind::InvalidData.into());
}
let data_len = data.len().saturating_sub(size_of::<u32>());
let crc = u32::from_le_bytes(data[data_len..].try_into().unwrap());
let check = crc32.check(&data[..data_len]);
if crc != check {
return Err(io::ErrorKind::InvalidData.into());
}
}
Ok(TrieDictionary { info, dict, data })
}
Expand Down
Loading

0 comments on commit 5fc999c

Please sign in to comment.