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

fix: correct Java 8 string handling to encode Rust strings as UTF-16 #153

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 3 additions & 7 deletions ristretto_vm/src/java_object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::Error::InternalError;
use crate::{Result, VM};
use ristretto_classfile::{mutf8, Version};
use ristretto_classloader::Error::ParseError;
use ristretto_classloader::{Class, Object, Reference, Value};
use ristretto_classloader::{Class, ConcurrentVec, Object, Reference, Value};
use std::sync::Arc;

const JAVA_8: Version = Version::Java8 { minor: 0 };
Expand Down Expand Up @@ -190,13 +189,10 @@
// In Java 9 and later, the value field is a byte array.
let java_class_file_version = vm.java_class_file_version();
let array = if java_class_file_version <= &JAVA_8 {
let bytes = mutf8::to_bytes(self)?;
let utf8_string =
String::from_utf8(bytes).map_err(|error| ParseError(error.to_string()))?;
let chars: Vec<char> = utf8_string.chars().collect();
Reference::from(chars)
let chars = self.encode_utf16().collect::<Vec<u16>>();
Reference::CharArray(ConcurrentVec::from(chars))
} else {
if java_class_file_version >= &JAVA_17 {

Check warning on line 195 in ristretto_vm/src/java_object.rs

View check run for this annotation

Codecov / codecov/patch

ristretto_vm/src/java_object.rs#L192-L195

Added lines #L192 - L195 were not covered by tests
object.set_value("hashIsZero", Value::Int(0))?;
}
object.set_value("coder", Value::Int(0))?; // LATIN1
Expand Down
6 changes: 4 additions & 2 deletions ristretto_vm/src/local_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ impl Display for LocalVariables {
continue;
};
let value = local.to_string();
if value.len() > 100 {
locals.push(format!("{}...", &value[..97]));
let chars: Vec<char> = value.chars().collect();
if chars.len() > 100 {
let value = chars.iter().take(97).collect::<String>();
locals.push(format!("{value}..."));
} else {
locals.push(value);
}
Expand Down
6 changes: 4 additions & 2 deletions ristretto_vm/src/operand_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@
let mut values = Vec::new();
for stack_entry in &stack {
let value = stack_entry.to_string();
if value.len() > 100 {
values.push(format!("{}...", &value[..97]));
let chars: Vec<char> = value.chars().collect();
if chars.len() > 100 {
let value = chars.iter().take(97).collect::<String>();
values.push(format!("{value}..."));

Check warning on line 158 in ristretto_vm/src/operand_stack.rs

View check run for this annotation

Codecov / codecov/patch

ristretto_vm/src/operand_stack.rs#L157-L158

Added lines #L157 - L158 were not covered by tests
} else {
values.push(value);
}
Expand Down