Skip to content

Commit

Permalink
Fix a crash error in debug build.
Browse files Browse the repository at this point in the history
* Fixed DateRewriter::IsNDigits to exclude negative values.
* #878

#codehealth

PiperOrigin-RevId: 633099245
  • Loading branch information
hiroyuki-komatsu committed May 13, 2024
1 parent a93e389 commit ba8751c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/data/test/session/scenario/t13n_negative_number.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file tests ConvertToHalfAscii against a negative number (F10 to -123)

# Enable IME
SEND_KEY ON
RESET_CONTEXT
SWITCH_INPUT_MODE HIRAGANA

################################################################################
# Test for MS-IME keymap
SET_CONFIG session_keymap MSIME

# Verify that input is learned.
SEND_KEYS -123
SEND_KEY F10
EXPECT_PREEDIT -123

RESET_CONTEXT
1 change: 1 addition & 0 deletions src/rewriter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ mozc_cc_library(
"//base:number_util",
"//base:util",
"//base:vlog",
"//base/strings:unicode",
"//composer",
"//converter:converter_interface",
"//converter:segments",
Expand Down
12 changes: 10 additions & 2 deletions src/rewriter/date_rewriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "base/clock.h"
#include "base/japanese_util.h"
#include "base/number_util.h"
#include "base/strings/unicode.h"
#include "base/util.h"
#include "base/vlog.h"
#include "composer/composer.h"
Expand Down Expand Up @@ -1168,8 +1169,15 @@ bool DateRewriter::ResizeSegments(const ConversionRequest &request,

namespace {
bool IsNDigits(const absl::string_view value, int n) {
return Util::CharsLen(value) == n &&
Util::GetScriptType(value) == Util::NUMBER;
if (Util::CharsLen(value) != n) {
return false;
}
for (absl::string_view c : Utf8AsChars(value)) {
if (Util::GetScriptType(c) != Util::NUMBER) {
return false;
}
}
return true;
}

// Gets n digits if possible.
Expand Down
25 changes: 25 additions & 0 deletions src/rewriter/date_rewriter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,31 @@ TEST_F(DateRewriterTest, ConsecutiveDigitsInsertPositionTest) {
}
}

TEST_F(DateRewriterTest, ConsecutiveDigitsWithMinusSign) {
commands::Request request;
const config::Config config;
const composer::Composer composer(nullptr, &request, &config);
const ConversionRequest conversion_request(&composer, &request, &config);

// Init an instance of Segments for this test.
Segments segments;
InitSegment("-123", "−123", &segments);

Segment *segment = segments.mutable_conversion_segment(0);
// Hiragana: ー is prolonged sound mark (U+2212)
segment->add_meta_candidate()->value = "ー123";
// Half Ascii: - is hyphen-minus (U+002D)
segment->add_meta_candidate()->value = "-123";
// Full Ascii: − is minus (U+30FC)
segment->add_meta_candidate()->value = "−123";
// Half Katakana: ー is half prolonged sound mark (U+FF70)
segment->add_meta_candidate()->value = "ー123";

// No rewrite is expected.
DateRewriter rewriter;
EXPECT_FALSE(rewriter.Rewrite(conversion_request, &segments));
}

TEST_F(DateRewriterTest, ConsecutiveDigitsInsertPositionWithHistory) {
commands::Request request;
const config::Config config;
Expand Down
1 change: 1 addition & 0 deletions src/rewriter/rewriter.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'<(mozc_oss_src_dir)/base/absl.gyp:absl_strings',
'<(mozc_oss_src_dir)/base/absl.gyp:absl_time',
'<(mozc_oss_src_dir)/base/base.gyp:base',
'<(mozc_oss_src_dir)/base/base.gyp:base_core',
'<(mozc_oss_src_dir)/base/base.gyp:config_file_stream',
'<(mozc_oss_src_dir)/base/base.gyp:serialized_string_array',
'<(mozc_oss_src_dir)/base/base.gyp:version',
Expand Down
1 change: 1 addition & 0 deletions src/session/session_handler_scenario_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const char *kScenarioFileList[] = {
DATA_DIR "segment_focus.txt",
DATA_DIR "segment_width.txt",
DATA_DIR "suggest_after_zero_query.txt",
DATA_DIR "t13n_negative_number.txt",
DATA_DIR "toggle_flick_hiragana_preedit_a.txt",
DATA_DIR "toggle_flick_hiragana_preedit_ka.txt",
DATA_DIR "toggle_flick_hiragana_preedit_sa.txt",
Expand Down

0 comments on commit ba8751c

Please sign in to comment.