thì kệ dev c :V hoặc tốt hơn là bỏ dev c chứ sao :V :V
có thể thêm flag -fsanitize=address
vào compiler cho nó check nhưng compiler cũ thì ko có flag này :V
hay em thử addtocollection 10 lần số 11 22 33 44 … rồi in ra tất cả các phần tử trong h
xem, bảo đảm ko đúng hết
đây a bị báo lỗi đây:
cấp phát động thì ok:
có -fsanitize=address
:
================================================================
==1==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffedf0c2dd8 at pc 0x000000401235 bp 0x7ffedf0c2d80 sp 0x7ffedf0c2d78
WRITE of size 8 at 0x7ffedf0c2dd8 thread T0
#0 0x401234 in AddToCollection /app/example.c:20
#1 0x401234 in main /app/example.c:28
#2 0x7f63896580b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)
#3 0x4012dd in _start (/app/output.s+0x4012dd)
Address 0x7ffedf0c2dd8 is located in stack of thread T0 at offset 72 in frame
#0 0x4010ff in main /app/example.c:25
This frame has 2 object(s):
[32, 40) 'h' (line 26)
[64, 76) 'arr' (line 15) <== Memory access at offset 72 partially overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /app/example.c:20 in AddToCollection
Shadow bytes around the buggy address:
0x10005be10560: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be10570: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be10580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be10590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be105a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x10005be105b0: 00 00 f1 f1 f1 f1 00 f2 f2 f2 00[04]f3 f3 00 00
0x10005be105c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be105d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be105e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be105f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x10005be10600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application 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
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==1==ABORTING
nó báo lỗi memory access ở biến ‘arr’ ngay :V :V
tên lỗi là “stack-buffer-overflow”, nghĩa là nó truy cập vùng nhớ nằm ngoài (overflow) stack :V
ủa hình như ko phải :V :V nó báo lỗi này là do xử lý int[3]
như là struct t_node
, nếu sửa lại là
struct t_node arr;
thì nó ko báo lỗi nữa, có fsanitize=address
cũng ko thấy báo lỗi :V :V :V
nhưng nếu sửa code lại thành
#include<stdio.h>
struct t_node
{
int item;
struct t_node *next;
};
typedef struct t_node *Node;
struct collection
{
Node head;
};
typedef struct collection collect;
void AddToCollection(collect *c, int item)
{
struct t_node arr;
//Node new; // CASE 1 khong ra kq gi
Node new = &arr; //CASE 2 in ra 10
//Node new = malloc(sizeof(struct t_node));// CASE 3 in ra 10
new->item = item;
new->next = c->head;
c->head = new;
}
int main()
{
collect h;
int a = 10;
AddToCollection(&h, 10);
AddToCollection(&h, 11);
AddToCollection(&h, 22);
AddToCollection(&h, 33);
AddToCollection(&h, 44);
printf("%d",h.head->item);
printf("%d",h.head->next->item);
printf("%d",h.head->next->next->item);
printf("%d",h.head->next->next->next->item);
printf("%d",h.head->next->next->next->next->item);
}
nó cũng chạy ko báo lỗi nhưng trên godbolt nó in ra 4444444444
:V còn ở rextester thì nó vẫn báo lỗi invalid memory reference :V
chứng tỏ 1 vài OS nó ko kiểm tra stack hay sao í :V biến arr
local tuy bị thu hồi nhưng giá trị nó chứa lúc trước khi bị thu hồi vẫn ko bị thay đổi sau khi thu hồi.
edit nữa: =]] nãy thả flag -g
nó ko báo lỗi, nhưng để flag -O2
nó lại báo lỗi mới lạ :V :V
================================================================
==1==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffe26a60060 at pc 0x00000040121e bp 0x7ffe26a60010 sp 0x7ffe26a60008
READ of size 4 at 0x7ffe26a60060 thread T0
#0 0x40121d in main /app/example.c:29
#1 0x7f8708ec50b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)
#2 0x4012dd in _start (/app/output.s+0x4012dd)
Address 0x7ffe26a60060 is located in stack of thread T0 at offset 64 in frame
#0 0x4010ff in main /app/example.c:25
This frame has 2 object(s):
[32, 40) 'h' (line 26)
[64, 80) 'arr' (line 15) <== Memory access at offset 64 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
lần này thì báo đúng lỗi “stack-use-after-scope” nghĩa là sử dụng biến trên stack sau khi đã ra khỏi phạm vi (đã bị thu hồi)