Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

引数のVoicevoxUserDictWord*がunalignedであることを許す #601

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ pub struct VoicevoxUserDict {
}

/// ユーザー辞書の単語。
#[derive(Clone, Copy)]
Copy link
Member Author

@qryxip qryxip Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは実は無くても動く(初めて知った)、がread(_unaligned)CopyしてはいけないオブジェクトをCopyしてしまったら流石にUBなので、ドキュメンテーション的な観点から付けておくことにする。

read creates a bitwise copy of T, regardless of whether T is Copy. If T is not Copy, using both the returned value and the value at *src can violate memory safety. Note that assigning to *src counts as a use because it will attempt to drop the value at *src.

#[repr(C)]
pub struct VoicevoxUserDictWord {
/// 表記
Expand Down Expand Up @@ -1056,11 +1057,11 @@ pub unsafe extern "C" fn voicevox_user_dict_load(
#[no_mangle]
pub unsafe extern "C" fn voicevox_user_dict_add_word(
user_dict: &VoicevoxUserDict,
word: &VoicevoxUserDictWord, // FIXME: <https://github.com/VOICEVOX/voicevox_core/pull/534>に従う
word: *const VoicevoxUserDictWord,
output_word_uuid: NonNull<[u8; 16]>,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let word = word.try_into_word()?;
let word = word.read_unaligned().try_into_word()?;
let uuid = {
let mut dict = user_dict.dict.lock().expect("lock failed");
dict.add_word(word)?
Expand All @@ -1087,11 +1088,11 @@ pub unsafe extern "C" fn voicevox_user_dict_add_word(
pub unsafe extern "C" fn voicevox_user_dict_update_word(
user_dict: &VoicevoxUserDict,
word_uuid: &[u8; 16],
word: &VoicevoxUserDictWord, // FIXME: <https://github.com/VOICEVOX/voicevox_core/pull/534>に従う
word: *const VoicevoxUserDictWord,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let word_uuid = Uuid::from_slice(word_uuid).map_err(CApiError::InvalidUuid)?;
let word = word.try_into_word()?;
let word = word.read_unaligned().try_into_word()?;
{
let mut dict = user_dict.dict.lock().expect("lock failed");
dict.update_word(word_uuid, word)?;
Expand Down