Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have string == ".......oo...ooo...o" it has this kind of value.I have to create int array that similar to string. For ex if the first index of string is '.' I have so assign 1 val to first index of int array .if I have string array like that "...o..o" i have to create int array with like that value {1, 1, 1, 0, 1, 1, 0} but if I do it with my code it is not working

What I have tried:

void my_bsq(int size, char** array){
    char*name = take_name(size, array);
    char* content = take_content(name);
    int measure = find_size(content), total_util = measure * measure + 1;
    int matrix[total_util], index = 0;
    for(int i = 0; i < total_util + measure- 2; i++){
        if(content[i] == 'o'){
             matrix[index++] = 0;
        }
        else if(content[i] == '.')  matrix[index++] = 1;
    }
    
}
Posted
Updated 5-Mar-23 11:00am
Comments
Irodabonu Komilova 28-Mar-23 9:51am    
=================================================================
==14118==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000059 at pc 0x55c8d9e2a76e bp 0x7ffe164bf5d0 sp 0x7ffe164bf5c0
READ of size 1 at 0x603000000059 thread T0
#0 0x55c8d9e2a76d in my_bsq /home/docode/project/my_bsq.c:182
#1 0x55c8d9e2ae04 in main /home/docode/project/my_bsq.c:238
#2 0x7f7415e5e082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
#3 0x55c8d9e2930d in _start (/home/docode/project/my_bsq+0x130d)

0x603000000059 is located 0 bytes to the right of 25-byte region [0x603000000040,0x603000000059)
allocated by thread T0 here:
#0 0x7f7416139808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x55c8d9e298c2 in str_make_index /home/docode/project/my_bsq.c:64
#2 0x55c8d9e2a3cc in my_bsq /home/docode/project/my_bsq.c:174
#3 0x55c8d9e2ae04 in main /home/docode/project/my_bsq.c:238
#4 0x7f7415e5e082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/docode/project/my_bsq.c:182 in my_bsq
Shadow bytes around the buggy address:
0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c067fff8000: fa fa fd fd fd fd fa fa 00 00 00[01]fa fa fa fa
0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
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
==14118==ABORTING



///it is showing this

C++
char* pText = "..ooo..o.o";
int arraylength = strlen(pText);
int* array = (int*)malloc(arraylength * sizeof(int));
int* pInt = array;
while (*pText)
{
    if (*pText == '.')
        *pInt = 1;
    if (*pText == 'o')
        *pInt = 0;
    pText++;
    pInt++;
}

You also need to check for illegal characters in the string
 
Share this answer
 
v2
Comments
OriginalGriff 5-Mar-23 4:30am    
:cough: increments :cough:
Richard MacCutchan 5-Mar-23 6:51am    
Oh excrement!
Memo to self: test your code.
Irodabonu Komilova 5-Mar-23 8:08am    
thank you very much
KarstenK 5-Mar-23 13:29pm    
you missed the else and error handling. This wouldnt pass my code review ;-)
Richard MacCutchan 6-Mar-23 3:48am    
No, I deliberately left it for the OP as mentioned in the final comment.
A function to do this conversion might look like this:
C
//void my_bsq(int size, char** array) {
void my_bsq(char* txt, int** array, int* size) 
{
    int len= strlen(txt);
    int* myarr = (int*)malloc(len * sizeof(int));
    if (!myarr) {
        *size = 0; return;
    }
    for (int i=0; i <len; i++) {
        switch (txt[i]) {
          case '.':  myarr[i] = 1; break;
          case  'o': // ... TODO
 
Share this answer
 
Comments
Irodabonu Komilova 28-Mar-23 9:51am    
=================================================================
==14118==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000059 at pc 0x55c8d9e2a76e bp 0x7ffe164bf5d0 sp 0x7ffe164bf5c0
READ of size 1 at 0x603000000059 thread T0
#0 0x55c8d9e2a76d in my_bsq /home/docode/project/my_bsq.c:182
#1 0x55c8d9e2ae04 in main /home/docode/project/my_bsq.c:238
#2 0x7f7415e5e082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
#3 0x55c8d9e2930d in _start (/home/docode/project/my_bsq+0x130d)

0x603000000059 is located 0 bytes to the right of 25-byte region [0x603000000040,0x603000000059)
allocated by thread T0 here:
#0 0x7f7416139808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x55c8d9e298c2 in str_make_index /home/docode/project/my_bsq.c:64
#2 0x55c8d9e2a3cc in my_bsq /home/docode/project/my_bsq.c:174
#3 0x55c8d9e2ae04 in main /home/docode/project/my_bsq.c:238
#4 0x7f7415e5e082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/docode/project/my_bsq.c:182 in my_bsq
Shadow bytes around the buggy address:
0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c067fff8000: fa fa fd fd fd fd fa fa 00 00 00[01]fa fa fa fa
0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
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
==14118==ABORTING////




it is showing this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900