Home > Back-end >  Leetcode palindrome bunch of issue to heap - buffer overflow
Leetcode palindrome bunch of issue to heap - buffer overflow

Time:10-25

This error is in the place of the output_s use malloc, following a dynamic application space online search similar mistake saw no find out problems,
Tell me where is wrong genuflect is begged

 char * longestPalindrome (char * s) {
Int a head, tail;
Int head_position tail_position;
Int output_head_position=0, output_tail_position=0;
Int max_length=0;
int length;
int i, j;
Int x, y;
Int k, l;

Int size=strlen (s);
Char * output_s=(char *) malloc (sizeof (char) * size);


For (I=0; I & lt; The size; I++)
{
The head [I]=s;
Head_position=I;

For (j=size - 1; J & gt; i; J -)
{
If (head==s [j]) {
Tail=s [j];
Length=2;
Tail_position=j;

For (x=I + 1, y=j - 1; X & lt;=y; X++, y -)
{
If (s==s [y] [x] & amp; & X!!!=y) {
Length +=2;
{} else if (x==y)
Length +=1;
} else if (s [x]!=s [y]) {
Length=0;
break;
}
}

If (length & gt; Max_length) {
Max_length=length;
Output_head_position=head_position;
Output_tail_position=tail_position;
}
}
}
}

For (k=output_head_position, l=0; K & lt;=output_tail_position; K++ l++)
{
Output_s [k] [l]=s;
}

Return output_s;
}


Error message is as follows:
=================================================================
====29 ERROR: AddressSanitizer: heap - buffer overflow on the address 0 x60200000003a at PC bp x7ffc39ee0860 sp 0 0 0 x00000040217c x7ffc39ee0858
The READ of the size 1 at 0 x60200000003a thread T0
# 3 0 x7fd69ad252e0 __libc_start_main in (/lib/x86_64 - - the gnu/Linux libc. So. 6 + 0 x202e0)
0 x60200000003a is located 0 bytes to the right of 10 - byte region [0 x602000000030, 0 x60200000003a)
Allocated by the thread T0 here:
# 0 0 x7fd69c5d42b0 in malloc (/usr/local/lib64/libasan. So. 5 + 0 xe82b0)
# 3 0 x7fd69ad252e0 __libc_start_main in (/lib/x86_64 - - the gnu/Linux libc. So. 6 + 0 x202e0)
Shadow bytes around the buggy address:
0 x0c047fff7fb0:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 x0c047fff7fc0:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 x0c047fff7fd0:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 x0c047fff7fe0:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0 x0c047fff7ff0:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=& gt; 00 0 x0c047fff8000: fa fa 06 fa fa fa [02] fa fa fa fa fa fa fa fa
0 x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0 x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0 x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0 x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0 x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one Shadow byte represents application 8 bytes) :
Addressable: 00
Partially addressable: 01, 02, 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
The Stack right redzone: f3
The Stack after return: f5
Stack the use after the scope: f8
Global redzone: f9
Global init order: f6
Poisoned by the user: f7
The Container overflow: fc
Array cookies: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
====29 ABORTING

CodePudding user response:

 for (x=I + 1, y=j - 1; X & lt;=y; X++, y -) 
16 {
17 if (s==s [y] [x] & amp; & X!!!=y) {
Length of 18 +=2;
19} else if (x==y) {
20 length +=1;
21} else if (s [x]!=s [y]) {
22 length=0;
23 break;
24}
25}

The building Lord have given, when the outer loop I=size - 1, x=I + 1, is x=the size has been crossed

CodePudding user response:

The
reference 1/f, confident boy reply:
 for (x=I + 1, y=j - 1; X & lt;=y; X++, y -) 
16 {
17 if (s==s [y] [x] & amp; & X!!!=y) {
Length of 18 +=2;
19} else if (x==y) {
20 length +=1;
21} else if (s [x]!=s [y]) {
22 length=0;
23 break;
24}
25}

The building Lord have given, when the outer loop I=size - 1, x=I + 1, x=the size has been crossed.

In addition, x++ continues to grow

CodePudding user response:

Heep is heap, cross-border, overrun

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related