cn-uofbasel / ccn-lite

CCN-lite, a lightweight implementation of the CCNx protocol and its variations
ISC License
74 stars 63 forks source link

potential memory leak in main() in ccn-lite-ctrl.c #327

Closed mfrey closed 5 years ago

mfrey commented 5 years ago

There is a potential memory leak in ccn-lite-ctrl.cidentified by scan-build.sh/clang static analyzer

1052   udp = strdup(optarg);
...
1055   port = strtol(strtok(NULL, "/"), NULL, 0);
1056   use_udp = 1;
1057   printf("udp: <%s> <%d>\n", udp, port);
1058   break;

EDIT: Originally posting was about strok/strtol. Hence, the first seven replies can be ignored for this issue.

blacksheeep commented 5 years ago

I think the purpose is get from 127.0.0.1/9000 to "char *udp=127.0.0.1" and "int port=9000".

udp = strtok(udp, "/"); deliveres the first part, while strtok(NULL, "/") deliveres the second part.

Note, that if the first parameter is a null pointer the function continues scanning wehere a previous successful call ended. Therefore, you cannot execute strtok with NULL as first parameter, without the overall context as you did in your example code. This is by design of the stdlib. However, there is a check missing, if udp is NULL, strtok must not be called with NULL as first parameter.

blacksheeep commented 5 years ago

For Reference see: http://www.cplusplus.com/reference/cstring/strtok/

mfrey commented 5 years ago

I've missed that. Can you just delete the issue?

blacksheeep commented 5 years ago

we still have the check of udp missing, should we create a new issue for that?

mfrey commented 5 years ago

i'm already writing the fix

we still have the check of udp missing, should we create a new issue for that?

I'm already writing the fix. PR will be up in a few minutes

blacksheeep commented 5 years ago

hmmm. I am not authorized to delete an issue.

mfrey commented 5 years ago

hmmm. I am not authorized to delete an issue.

Okay. I'm going to add some background on the memory leak and give this issue a proper title.