-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix length stored by substr() and optimize the implementation
In certain cases, substr() would store an incorrect length for the result string. This problem is resolved with this patch. Better comments have been added as well to make the code easier to follow. The code handling the case where (idx < 0) failed to recognize that when the adjusted idx value was more negative than the value of cnt, the result would always be the empty string. This has been corrected which improved code performance as well. Another important change can be found in label .Lcheck_idx (formerly .Ladjust_cnt). The original four conditionals were overkill, and the case for (cnt > 0) was jumping to .Lcnt_pos incorrectly (causing the incorrect string length value). It now correctly jumps to .Lcopy. In fact, the .Lcnt_pos code was unnecessary. Copying the substring now makes use of the probe_read_str() helper rather than the probe_read() helper. 10 new tests are added to test various special conditions. Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com> Reviewed-by: Eugene Loh <eugene.loh@oracle.com>
- Loading branch information
Showing
21 changed files
with
550 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-no-cnt.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Oracle Linux DTrace. | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* http://oss.oracle.com/licenses/upl. | ||
*/ | ||
|
||
/* | ||
* ASSERTION: The substr() subroutine supports negative index values passed as | ||
* constants. | ||
* | ||
* SECTION: Actions and Subroutines/substr() | ||
*/ | ||
|
||
#pragma D option quiet | ||
|
||
BEGIN | ||
{ | ||
printf("\n% 3d %s", -10, substr("abcdefghijklmnop", -10)); | ||
printf("\n% 3d %s", -9, substr("abcdefghijklmnop", -9)); | ||
printf("\n% 3d %s", -8, substr("abcdefghijklmnop", -8)); | ||
printf("\n% 3d %s", -7, substr("abcdefghijklmnop", -7)); | ||
printf("\n% 3d %s", -6, substr("abcdefghijklmnop", -6)); | ||
printf("\n% 3d %s", -5, substr("abcdefghijklmnop", -5)); | ||
printf("\n% 3d %s", -4, substr("abcdefghijklmnop", -4)); | ||
printf("\n% 3d %s", -3, substr("abcdefghijklmnop", -3)); | ||
printf("\n% 3d %s", -2, substr("abcdefghijklmnop", -2)); | ||
printf("\n% 3d %s", -1, substr("abcdefghijklmnop", -1)); | ||
exit(0); | ||
} |
11 changes: 11 additions & 0 deletions
11
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-no-cnt.r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
-10 ghijklmnop | ||
-9 hijklmnop | ||
-8 ijklmnop | ||
-7 jklmnop | ||
-6 klmnop | ||
-5 lmnop | ||
-4 mnop | ||
-3 nop | ||
-2 op | ||
-1 p |
30 changes: 30 additions & 0 deletions
30
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-too-far-no-cnt.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Oracle Linux DTrace. | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* http://oss.oracle.com/licenses/upl. | ||
*/ | ||
|
||
/* | ||
* ASSERTION: The substr() subroutine supports negative index values passed as | ||
* constants, with some values out of range. | ||
* | ||
* SECTION: Actions and Subroutines/substr() | ||
*/ | ||
|
||
#pragma D option quiet | ||
|
||
BEGIN | ||
{ | ||
printf("\n% 3d %s", -10, substr("abcde", -10)); | ||
printf("\n% 3d %s", -9, substr("abcde", -9)); | ||
printf("\n% 3d %s", -8, substr("abcde", -8)); | ||
printf("\n% 3d %s", -7, substr("abcde", -7)); | ||
printf("\n% 3d %s", -6, substr("abcde", -6)); | ||
printf("\n% 3d %s", -5, substr("abcde", -5)); | ||
printf("\n% 3d %s", -4, substr("abcde", -4)); | ||
printf("\n% 3d %s", -3, substr("abcde", -3)); | ||
printf("\n% 3d %s", -2, substr("abcde", -2)); | ||
printf("\n% 3d %s", -1, substr("abcde", -1)); | ||
exit(0); | ||
} |
11 changes: 11 additions & 0 deletions
11
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-too-far-no-cnt.r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
-10 abcde | ||
-9 abcde | ||
-8 abcde | ||
-7 abcde | ||
-6 abcde | ||
-5 abcde | ||
-4 bcde | ||
-3 cde | ||
-2 de | ||
-1 e |
30 changes: 30 additions & 0 deletions
30
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-too-far.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Oracle Linux DTrace. | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* http://oss.oracle.com/licenses/upl. | ||
*/ | ||
|
||
/* | ||
* ASSERTION: The substr() subroutine supports negative index values passed as | ||
* constants, with some values out of range. | ||
* | ||
* SECTION: Actions and Subroutines/substr() | ||
*/ | ||
|
||
#pragma D option quiet | ||
|
||
BEGIN | ||
{ | ||
printf("\n% 3d % 3d %s", -10, 3, substr("abcde", -10, 3)); | ||
printf("\n% 3d % 3d %s", -9, 3, substr("abcde", -9, 3)); | ||
printf("\n% 3d % 3d %s", -8, 3, substr("abcde", -8, 3)); | ||
printf("\n% 3d % 3d %s", -7, 3, substr("abcde", -7, 3)); | ||
printf("\n% 3d % 3d %s", -6, 3, substr("abcde", -6, 3)); | ||
printf("\n% 3d % 3d %s", -5, 3, substr("abcde", -5, 3)); | ||
printf("\n% 3d % 3d %s", -4, 3, substr("abcde", -4, 3)); | ||
printf("\n% 3d % 3d %s", -3, 3, substr("abcde", -3, 3)); | ||
printf("\n% 3d % 3d %s", -2, 3, substr("abcde", -2, 3)); | ||
printf("\n% 3d % 3d %s", -1, 3, substr("abcde", -1, 3)); | ||
exit(0); | ||
} |
11 changes: 11 additions & 0 deletions
11
test/unittest/funcs/substr/tst.substr-multi-const-idx-neg-too-far.r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
-10 3 | ||
-9 3 | ||
-8 3 | ||
-7 3 a | ||
-6 3 ab | ||
-5 3 abc | ||
-4 3 bcd | ||
-3 3 cde | ||
-2 3 de | ||
-1 3 e |
31 changes: 31 additions & 0 deletions
31
test/unittest/funcs/substr/tst.substr-multi-const-idx-pos-no-cnt.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Oracle Linux DTrace. | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* http://oss.oracle.com/licenses/upl. | ||
*/ | ||
|
||
/* | ||
* ASSERTION: The substr() subroutine supports positive index values passed as | ||
* constants. | ||
* | ||
* SECTION: Actions and Subroutines/substr() | ||
*/ | ||
|
||
#pragma D option quiet | ||
|
||
BEGIN | ||
{ | ||
printf("\n% 3d %s", 0, substr("abcdefghijklmnop", 0)); | ||
printf("\n% 3d %s", 1, substr("abcdefghijklmnop", 1)); | ||
printf("\n% 3d %s", 2, substr("abcdefghijklmnop", 2)); | ||
printf("\n% 3d %s", 3, substr("abcdefghijklmnop", 3)); | ||
printf("\n% 3d %s", 4, substr("abcdefghijklmnop", 4)); | ||
printf("\n% 3d %s", 5, substr("abcdefghijklmnop", 5)); | ||
printf("\n% 3d %s", 6, substr("abcdefghijklmnop", 6)); | ||
printf("\n% 3d %s", 7, substr("abcdefghijklmnop", 7)); | ||
printf("\n% 3d %s", 8, substr("abcdefghijklmnop", 8)); | ||
printf("\n% 3d %s", 9, substr("abcdefghijklmnop", 9)); | ||
printf("\n% 3d %s", 10, substr("abcdefghijklmnop", 10)); | ||
exit(0); | ||
} |
12 changes: 12 additions & 0 deletions
12
test/unittest/funcs/substr/tst.substr-multi-const-idx-pos-no-cnt.r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
0 abcdefghijklmnop | ||
1 bcdefghijklmnop | ||
2 cdefghijklmnop | ||
3 defghijklmnop | ||
4 efghijklmnop | ||
5 fghijklmnop | ||
6 ghijklmnop | ||
7 hijklmnop | ||
8 ijklmnop | ||
9 jklmnop | ||
10 klmnop |
40 changes: 40 additions & 0 deletions
40
test/unittest/funcs/substr/tst.substr-multi-var-idx-neg-no-cnt.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Oracle Linux DTrace. | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Licensed under the Universal Permissive License v 1.0 as shown at | ||
* http://oss.oracle.com/licenses/upl. | ||
*/ | ||
|
||
/* | ||
* ASSERTION: The substr() subroutine supports negative index values passed by | ||
* variable. | ||
* | ||
* SECTION: Actions and Subroutines/substr() | ||
*/ | ||
|
||
#pragma D option quiet | ||
|
||
BEGIN | ||
{ | ||
idx = -10; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -9; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -8; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -7; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -6; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -5; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -4; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -3; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -2; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
idx = -1; | ||
printf("\n% 3d %s", idx, substr("abcdefghijklmnop", idx)); | ||
exit(0); | ||
} |
Oops, something went wrong.