how to free a malloced pointer after a memmove

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



how to free a malloced pointer after a memmove



apparently, according to what is the difference between free(a) and memset(a, 0, malloced_size) (actual title is "Free function in c" but i feel that is not specific enough), i need to memset the data to 0 before/after i free it to actually free it as if it was never assigned any data



UPDATE: fixed


#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdio.h>

uintptr_t round_up(uintptr_t value, uintptr_t size)

fprintf(stderr, "called round_upnreturning %pn", (void *) (value ? size * ((value + (size - 1)) / size) : size));
return value ? size * ((value + (size - 1)) / size) : size;


int read_fast_verify(const char *src, int len_of_source, char **dest, char ** a, int requested_len)
*a = malloc(requested_len+4096);
if (len_of_source < requested_len) memcpy(*a, src, len_of_source);
else memcpy(*a, src, requested_len);
*dest = memmove((void *)round_up((uintptr_t)*a, 4096), src, requested_len);
return requested_len;


void __lseek_string__(char **src, int len, int offset)
memmove(*src, *src+offset, len);


char * string1 = "hello";
char * string2;
char * s;
int main(void)
read_fast_verify(string1, strlen(string1), &string2, &s, (strlen(string1) + 5));
__lseek_string__(&string2, strlen(string1), 5);
free(s);



How do I free a malloced pointer after I memmove it, as memmove seems to seg fault if I use a NULL dest as its return (eg char * dest = memmove(...);)


#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdio.h>

uintptr_t round_up(uintptr_t value, uintptr_t size)

fprintf(stderr, "called round_upnreturning %pn", (void *) (value ? size * ((value + (size - 1)) / size) : size));
return value ? size * ((value + (size - 1)) / size) : size;


int read_fast_verify(const char *src, int len_of_source, char **dest, char ** a, int requested_len)
*a = malloc(requested_len+4096);
if (len_of_source < requested_len) memcpy(*a, src, len_of_source);
else memcpy(*a, src, requested_len);
*dest = memmove((void *)round_up((uintptr_t)*a, 4096), *dest, requested_len);
return requested_len;


void __lseek_string__(char **src, int len, int offset)
memmove(*src, *src+offset, len);


char * string1 = "hello";
char * string2;
char * s;
int main(void)
read_fast_verify(string1, strlen(string1), &string2, &s, (strlen(string1) + 5));
__lseek_string__(&string2, strlen(string1), 5);
free(s);



the output


Starting program: /home/arch/universal-dynamic-loader/loader/test_case
called round_up
returning 0x55555555a000

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f2d3b0 in __memmove_ssse3_back () from /usr/lib/libc.so.6



However if I just do it as normal


#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdio.h>

uintptr_t round_up(uintptr_t value, uintptr_t size)

fprintf(stderr, "called round_upnreturning %pn", (void *) (value ? size * ((value + (size - 1)) / size) : size));
return value ? size * ((value + (size - 1)) / size) : size;


int read_fast_verify(const char *src, int len_of_source, char **dest, char ** a, int requested_len)
*dest = malloc(requested_len+4096);
if (len_of_source < requested_len) memcpy(*dest, src, len_of_source);
else memcpy(*dest, src, requested_len);
*dest = memmove((void *)round_up((uintptr_t)*dest, 4096), *dest, requested_len);
return requested_len;


void __lseek_string__(char **src, int len, int offset)
memmove(*src, *src+offset, len);


char * string1 = "hello";
char * string2;
char * s;
int main(void)
read_fast_verify(string1, strlen(string1), &string2, &s, (strlen(string1) + 5));
__lseek_string__(&string2, strlen(string1), 5);
free(string2);



then I get this


called round_up
returning 0x564c86f54000
free(): invalid pointer
Aborted (core dumped)



and from valgrind


==19175== HEAP SUMMARY:
==19175== in use at exit: 4,106 bytes in 1 blocks
==19175== total heap usage: 1 allocs, 1 frees, 4,106 bytes allocated
==19175==
==19175== Searching for pointers to 1 not-freed blocks
==19175== Checked 68,008 bytes
==19175==
==19175== 4,106 bytes in 1 blocks are possibly lost in loss record 1 of 1
==19175== at 0x4837757: malloc (vg_replace_malloc.c:299)
==19175== by 0x10923C: read_fast_verify (test_case.c:13)
==19175== by 0x109357: main (test_case.c:28)
==19175==
==19175== LEAK SUMMARY:
==19175== definitely lost: 0 bytes in 0 blocks
==19175== indirectly lost: 0 bytes in 0 blocks
==19175== possibly lost: 4,106 bytes in 1 blocks
==19175== still reachable: 0 bytes in 0 blocks
==19175== suppressed: 0 bytes in 0 blocks
==19175==
==19175== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==19175==
==19175== 1 errors in context 1 of 2:
==19175== Invalid free() / delete / delete / realloc()
==19175== at 0x4838904: free (vg_replace_malloc.c:530)
==19175== by 0x109388: main (test_case.c:30)
==19175== Address 0x4a30000 is 4,032 bytes inside a block of size 4,106 alloc'd
==19175== at 0x4837757: malloc (vg_replace_malloc.c:299)
==19175== by 0x10923C: read_fast_verify (test_case.c:13)
==19175== by 0x109357: main (test_case.c:28)
==19175==
==19175== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)



as I am trying to make it memory safe as my dynamic linker is currently using over 2.7 gb (and over 4 GB shared memory) when reading a .so (such as libc 2.28) which it should not be at all.





memmove((void *)round_up((uintptr_t)*a, 4096), *dest, ...: memmove() copies from where its 2nd parameter points to. *dest points nowhere. So shouldn't this be *dest = memmove((void *)round_up((uintptr_t)*a, 4096), *a, ...?
– alk
Aug 12 at 13:47



memmove((void *)round_up((uintptr_t)*a, 4096), *dest, ...


memmove()


*dest


*dest = memmove((void *)round_up((uintptr_t)*a, 4096), *a, ...





@alk so should i do '*dest = memmove((void *)round_up((uintptr_t)*a, 4096), *src, requested_len);' ?
– PSP CODER
Aug 12 at 13:56





You must free whatever malloc and friends return, and nothing else. If you try to free something else, your program may crash, print an error message, or steal your money and your girlfriend. string2 isn't something that malloc returns. What do you expect free to do with it?
– n.m.
Aug 12 at 14:14



free


malloc


free


string2


malloc


free





somehow it is still using 3 gb of memory even tho i am freeing everything and valgrind shows 0 of 0 leaks
– PSP CODER
Aug 12 at 15:09





Note that you should not create function, variable or macro names that start with an underscore, in general. C11 §7.1.3 Reserved identifiers says (in part): — All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces. See also What does double underscore (__const) mean in C?
– Jonathan Leffler
Aug 12 at 16:00


__const




1 Answer
1



You should be storing the previous value and freeing after the move completed successfully. Right now you just throw away the previous address.



Also not sure you need to move here in this example as you'll always point to a relative addrss within the first pointer and return that, there's no pointer to free but the origin pointer.



See also Does memmove actually "move" a chunk of memory and leave behind zeros at the source?





updated question
– PSP CODER
Aug 12 at 13:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered