0

I am developing a program in Contiki OS .I attempt to copy an input of a function to a char array and then concatenate a dynamic char array to another. The destination return NULL value. here is the code of generating log value which is the input of makerequest function:

   char *log;
   log = (char *)malloc(400);
   if(uip_newdata()) {
     strncat(log, (char *)uip_appdata ,uip_datalen());
    }
   makerequest(log);

and in makerequest function I tried to concatenate the req value to rcvReq :

   static char * rcvReq;       
   void makerequest(char * log){
        rcvReq = (char*)malloc(300);
        char * req;
        req = (char*)malloc(200);
        memset(req, 0, sizeof(req));
        strcpy(req, log);

        ...

        if(rcvReq != NULL){
           strncat(rcvReq, req, strlen(req));
          }
        else{
           strncpy(rcvReq, req, strlen(req));
          }
   }

req is a temp array to store log value. I expected that req value copied to rcvReq, but value of rcvReq is NULL. I would be really appreciate if you tell me how I can fix the problem.

mahshid
  • 41
  • 7
  • Add null checks after malloc(). Is `rcvReq = (char*)malloc(300);` allocated successfully? – MayurK Apr 26 '19 at 04:33
  • `if(rcvReq != NULL) {...} else { strncpy(rcvReq, req, strlen(req)) };` If you do strncpy when rcvReq is NULL, it will give undefined behavior. – MayurK Apr 26 '19 at 04:38
  • `memset(req, 0, sizeof(req));` Sizeof(req) will be size of pointer (may be 4 or 8) depending on your machine. You should do `memset(req, 0, 200);` – MayurK Apr 26 '19 at 04:39
  • `strncat(log, (char *)uip_appdata ,uip_datalen());` What if uip_datalen() returns more than 400? It should be `strncat(log, (char *)uip_appdata 400);` – MayurK Apr 26 '19 at 04:40
  • 2
    What you want in the first snippet of code is [**`memcpy`**](https://en.cppreference.com/w/c/string/byte/memcpy), not `strncat`. That's because the memory allocated by `malloc` is not initialized in any way, most important it's not a null-terminated byte string as expected by `strcat` (and `strncat`). And if the data you receive is not null-terminated either, then you can use e.g. `strncpy`. Also note that you can only copy **399** bytes into the memory you allocate, and that you explicitly have to add a null-terminator in the correct position. – Some programmer dude Apr 26 '19 at 04:49
  • 2
    Also, even if you have "character" arrays, what is the contents of the data you receive? Is it really *strings* (null-terminated byte strings)? Or is it arbitrary binary data? If it's not explicitly null-terminated strings you can't use *any* string functions are all. – Some programmer dude Apr 26 '19 at 04:53
  • 2
    Oh and unless you do `free(rcvReq)` somewhere else in your code, you have a nasty memory leak. Also in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/). – Some programmer dude Apr 26 '19 at 05:03
  • @MayurK thank you for all the suggestions. I will check them. but I have a question about the first one. I'm new to C so I don't know how I can make sure about the allocation. I used sizeof(rcvReq) and sizeof(req). both of them return 2. the value of log copied to req but it did not copy to rcvReq. – mahshid Apr 26 '19 at 05:10
  • When doing `sizeof` on a pointer (like e.g. `sizeof rcvReq`) then you get the size of the ***pointer itself***, not what it points to. You need to keep track of the size yourself. The way to check if `malloc` succeeded is to check if it returns `NULL` (failed) or not. – Some programmer dude Apr 26 '19 at 05:21
  • 1
    Regarding your allocations, on very small embedded systems (typically the target for Contiki) you really should try to avoid dynamic allocation. The heap is usually very limited, and doing a lot of dynamic allocations will fragment it leading to problems allocating further down the line, because even if the total amount of free memory is enough to satisfy the allocation there might not be a free chunk of the heap that is large enough. – Some programmer dude Apr 26 '19 at 05:23

0 Answers0