gpt4 book ai didi

java - 安卓漏洞? : String. substring(5).replace (“” , “” )//空字符串

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:25 25 4
gpt4 key购买 nike

这是我的代码:

String str = "just_a_string";
System.out.println("]" + str + "[");
System.out.println("]" + str.replace("", "") + "[");
System.out.println("]" + str.substring(5) + "[");
System.out.println("]" + str.substring(5).replace("", "") + "[");
System.out.println("]" + str.substring(3, 8) + "[");
System.out.println("]" + str.substring(3, 8).replace("", "") + "[");
System.out.println("]" + "sdajndan".substring(5).replace("", "") + "[");

这是输出

05-09 19:09:20.570: I/System.out(23801): ]just_a_string[
05-09 19:09:20.570: I/System.out(23801): ]just_a_string[
05-09 19:09:20.570: I/System.out(23801): ]a_string[
05-09 19:09:20.570: I/System.out(23801): ]a_s[ **
05-09 19:09:20.570: I/System.out(23801): ]t_a_s[
05-09 19:09:20.570: I/System.out(23801): ]t_[ **
05-09 19:09:20.570: I/System.out(23801): ][ **

显然,标有**的行是意想不到的。

此问题发生在我的 Android 手机 A(LG P920 Optimus 3D,Android 2.3.3)上。当我在我的 Android 手机 B(LG E720 Optimus Chic,Android 2.2)上进行测试时,它停止了。我猜它会进入无限循环。

我已经在使用 Java 1.51.6 的两部手机上进行了测试。两者分别导致相同的行为。

我还在我的同一个 Eclipse 上用 Java 项目测试了 1.51.61.7。正如预期的那样,他们的所有输出都是正确的。

我想知道这可能是针对字符串的 backing array 实现 String.replace(“”, “”) 的设备特定问题.

你能帮我测试一下你的设备吗?

谁能给我String.replace(CharSequence, CharSequence)方法的Android源代码? (就像 docjar 中的一样)

非常感谢!


我稍微修改了代码,所以它也可以在Android设备上显示。 (无论如何都是相同的代码)。

在我的手机 A 和手机 B 上进行了测试。行为仍然相同,如上所述。

package com.example.testprojectnew;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

String output_text = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String str = "just_a_string";
process("1]" + str + "[");
process("2]" + str.replace("", "") + "[");
process("3]" + str.substring(5) + "[");
process("4]" + str.substring(5).replace("", "") + "[");
process("5]" + str.substring(3, 8) + "[");
process("6]" + str.substring(3, 8).replace("", "") + "[");
process("7]" + "sdajndan".substring(5).replace("", "") + "[");

output_text = output_text.concat("\n\nLines (1 & 2), (3 & 4), (5 & 6), should be the same.");

((TextView) findViewById(R.id.a_string)).setText(output_text);
}
private void process(String str) {
System.out.println(str);
output_text = output_text.concat(str).concat("\n");
}
}

最佳答案

宾果!我找到了错误!

感谢@izht 提供 source code 的链接.我已经找到了关于这个问题的错误。

这只发生在字符串的 backing array 时。具有与实际字符串不同(更长)的值。特别是,当 String.offset(私有(private)变量)大于零时。

修复方法如下:

public String replace(CharSequence target, CharSequence replacement) {
if (target == null) {
throw new NullPointerException("target == null");
}
if (replacement == null) {
throw new NullPointerException("replacement == null");
}

String targetString = target.toString();
int matchStart = indexOf(targetString, 0);
if (matchStart == -1) {
// If there's nothing to replace, return the original string untouched.
return this;
}

String replacementString = replacement.toString();

// The empty target matches at the start and end and between each character.
int targetLength = targetString.length();
if (targetLength == 0) {
int resultLength = (count + 2) * replacementString.length();
StringBuilder result = new StringBuilder(resultLength);
result.append(replacementString);
// for (int i = offset; i < count; ++i) { // original, bug
for (int i = offset; i < (count + offset); ++i) { // fix
result.append(value[i]);
result.append(replacementString);
}
return result.toString();
}

StringBuilder result = new StringBuilder(count);
int searchStart = 0;
do {
// Copy characters before the match...
result.append(value, offset + searchStart, matchStart - searchStart);
// Insert the replacement...
result.append(replacementString);
// And skip over the match...
searchStart = matchStart + targetLength;
} while ((matchStart = indexOf(targetString, searchStart)) != -1);
// Copy any trailing chars...
result.append(value, offset + searchStart, count - searchStart);
return result.toString();
}

我不确定为什么 Android 必须以这种方式更改(并且错误地更改)replace()。原始的 Java 实现没有这个问题。

顺便问一下,现在怎么样了?我能用它做什么? (除非格外小心地使用 replace(),否则就扔掉我的 Android 手机 :-/)


顺便说一句,我很确定我的 LG E720 Optimus Chic (Android 2.2) 使用的源代码与 that one 不同。 .它会在 String.replace() 上用 目标字符串停止(怀疑是无限循环)。最近我发现它抛出这个错误信息:

05-10 16:41:13.155: E/AndroidRuntime(9384): FATAL EXCEPTION: main
05-10 16:41:13.155: E/AndroidRuntime(9384): java.lang.OutOfMemoryError
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:157)
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.StringBuilder.append(StringBuilder.java:217)
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.String.replace(String.java:1497)
05-10 16:41:13.155: E/AndroidRuntime(9384): at com.example.testprojectnew.MainActivity.onCreate(MainActivity.java:22)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.os.Looper.loop(Looper.java:123)
05-10 16:41:13.155: E/AndroidRuntime(9384): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 16:41:13.155: E/AndroidRuntime(9384): at java.lang.reflect.Method.invoke(Method.java:521)
05-10 16:41:13.155: E/AndroidRuntime(9384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
05-10 16:41:13.155: E/AndroidRuntime(9384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
05-10 16:41:13.155: E/AndroidRuntime(9384): at dalvik.system.NativeStart.main(Native Method)

再想一想,如果那个 for 循环 的错误。应该是编译时的问题。为什么它在不同的手机(不同版本的 Android)中表现不同?


完整的解决方法

得到 an update来自谷歌,他们已经修补了它,并将更正它 in the future release .

与此同时,我已经编写了一个基于their code 的补丁方法。 :

(这是必要的,因为 (1) 我们仍需等待正确的发布,(2) 我们需要处理未进行该固定更新的设备)

/** Patch for the String.replace(CharSequence target, CharSequence replacement),
* because the original is buggy when CharSequence target is empty, i.e. "".
* Patched by Google Android: https://android-review.googlesource.com/58393
*/
public static String replacePatched(final String string, final CharSequence target, final CharSequence replacement) {
if (target == null) {
throw new NullPointerException("target == null");
}
if (replacement == null) {
throw new NullPointerException("replacement == null");
}

final String targetString = target.toString();
int matchStart = string.indexOf(targetString, 0);
if (matchStart == -1) {
// If there's nothing to replace, return the original string untouched.
return new String(string);
}

final char[] value = string.toCharArray(); // required in patch
final int count = value.length; // required in patch

final String replacementString = replacement.toString();

// The empty target matches at the start and end and between each character.
if (targetString.length() == 0) {
// The result contains the original 'count' characters, a copy of the
// replacement string before every one of those characters, and a final
// copy of the replacement string at the end.
final StringBuilder result = new StringBuilder(count + (count + 1) * replacementString.length());
result.append(replacementString);
for (int i = 0; i < count; ++i) {
result.append(value[i]);
result.append(replacementString);
}
return new String(result); // StringBuilder.toString() does not give exact length
}

final StringBuilder result = new StringBuilder(count);
int searchStart = 0;
do {
// Copy characters before the match...
result.append(value, searchStart, matchStart - searchStart);
// Insert the replacement...
result.append(replacementString);
// And skip over the match...
searchStart = matchStart + targetString.length();
} while ((matchStart = string.indexOf(targetString, searchStart)) != -1);
// Copy any trailing chars...
result.append(value, searchStart, count - searchStart);
return new String(result); // StringBuilder.toString() does not give exact length
}

详细版本:

/** Patch for the String.replace(CharSequence target, CharSequence replacement),
* because the original is buggy when CharSequence target is empty, i.e. "".
* Patched by Google Android: https://android-review.googlesource.com/58393
*/
public static String replacePatched(final String string, final CharSequence target, final CharSequence replacement) {
if (target == null) {
throw new NullPointerException("target == null");
}
if (replacement == null) {
throw new NullPointerException("replacement == null");
}

// String targetString = target.toString(); // original
final String targetString = target.toString();
// int matchStart = indexOf(targetString, 0); // original
int matchStart = string.indexOf(targetString, 0);
if (matchStart == -1) {
// If there's nothing to replace, return the original string untouched.
// return this; // original
return new String(string);
}

final char[] value = string.toCharArray(); // required in patch
final int count = value.length; // required in patch

// String replacementString = replacement.toString(); // original
final String replacementString = replacement.toString();

// The empty target matches at the start and end and between each character.
// int targetLength = targetString.length(); // original
// if (targetLength == 0) { // original
if (targetString.length() == 0) {
// int resultLength = (count + 2) * replacementString.length(); // original
// // The result contains the original 'count' characters, a copy of the
// // replacement string before every one of those characters, and a final
// // copy of the replacement string at the end.
// int resultLength = count + (count + 1) * replacementString.length(); // patched by Google Android
// StringBuilder result = new StringBuilder(resultLength); // original
final StringBuilder result = new StringBuilder(count + (count + 1) * replacementString.length());
result.append(replacementString);
// for (int i = offset; i < count; ++i) { // original
// int end = offset + count; // patched by Google Android
// for (int i = offset; i != end; ++i) { // patched by Google Android
for (int i = 0; i < count; ++i) {
result.append(value[i]);
result.append(replacementString);
}
// return result.toString(); // original
return new String(result); // StringBuilder.toString() does not give exact length
}

// StringBuilder result = new StringBuilder(count); // original
final StringBuilder result = new StringBuilder(count);
int searchStart = 0;
do {
// Copy characters before the match...
// result.append(value, offset + searchStart, matchStart - searchStart); // original
result.append(value, searchStart, matchStart - searchStart);
// Insert the replacement...
result.append(replacementString);
// And skip over the match...
// searchStart = matchStart + targetLength; // original
searchStart = matchStart + targetString.length();
// } while ((matchStart = indexOf(targetString, searchStart)) != -1); // original
} while ((matchStart = string.indexOf(targetString, searchStart)) != -1);
// Copy any trailing chars...
// result.append(value, offset + searchStart, count - searchStart); // original
result.append(value, searchStart, count - searchStart);
// return result.toString(); // original
return new String(result); // StringBuilder.toString() does not give exact length
}

关于java - 安卓漏洞? : String. substring(5).replace (“” , “” )//空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com