New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify log in udpserver.c #1314
base: master
Are you sure you want to change the base?
Conversation
KangLin
commented
Nov 10, 2023
•
edited
edited
- Modify log in udpserver.c
- fix memory leak
f9b4cdc
to
7859523
Compare
New output contents: 10:42:45: Debugging /home/build-coturn-Desktop_Qt_5_12_12_GCC_64bit-Debug/bin/turnutils_peer -v -L 192.168.11.30 |
stun_buffer buffer; | ||
ioa_addr remote_addr; | ||
int slen = get_ioa_addr_len(&listen->addr); | ||
stun_buffer *buffer = (stun_buffer *)malloc(sizeof(stun_buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason to use heap instead of stack as before? It does not go ouside of the function and actually released at the end of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This size is large. then maybe stack overflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default stack sizes are large - usually 1MB
Here we are talking about packet size - 1500B? Did you have an issue?
Not that it matters much for a test util but I would still not include unexplained changes to code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with eakraly
the size of a stun packet shouldn't be large enough to justify always mallocing.
If you're concerned about stack size, you could try to support dynamic buffers, with a default buffer of 1500 on the stack, and then larger buffers allocated if necessary because the incoming data is large enough to justify it.
Though given the nature of async code, i'm not sure it would be possible to determine that prior to attempting to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To summarize, I have a fairly strong preference that this buffer be on the stack not on the heap, especially since I've spent the last week trying to track down memory leaks in coturn and C-language code is notorious for memory problems in general.
Default stack sizes are large - usually 1MB
Warning prompt during clang grammar checking. Some compilers are very small, only 516.
Whether it's a testing program or not, a good habit should be maintained.
At 2023-11-20 04:06:53, "Pavel Punsky" ***@***.***> wrote:
@eakraly commented on this pull request.
In src/apps/peer/udpserver.c:
int len = 0;
- int slen = get_ioa_addr_len(addr);
- stun_buffer buffer;
- ioa_addr remote_addr;
+ int slen = get_ioa_addr_len(&listen->addr);
+ stun_buffer *buffer = (stun_buffer *)malloc(sizeof(stun_buffer));
Default stack sizes are large - usually 1MB
Here we are talking about packet size - 1500B? Did you have an issue?
Not that it matters much for a test util but I would still not include unexplained changes to code
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
- fix memory leak
@eakraly pls merge! |
@@ -58,7 +58,7 @@ int main(int argc, char **argv) { | |||
char **local_addr_list = NULL; | |||
size_t las = 0; | |||
int verbose = TURN_VERBOSE_NONE; | |||
int c; | |||
int c = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can int c
be moved down in to the while loop instead ?
@@ -98,6 +98,8 @@ int main(int argc, char **argv) { | |||
} | |||
|
|||
server_type *server = start_udp_server(verbose, ifname, local_addr_list, las, port); | |||
if (!server) | |||
return -3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can these return codes be made into named constants?
int slen = get_ioa_addr_len(&listen->addr); | ||
stun_buffer *buffer = (stun_buffer *)malloc(sizeof(stun_buffer)); | ||
ioa_addr remote_addr = {0}; | ||
if (!buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braces please
|
||
do { | ||
len = recvfrom(fd, buffer.buf, sizeof(buffer.buf) - 1, 0, (struct sockaddr *)&remote_addr, (socklen_t *)&slen); | ||
len = recvfrom(fd, buffer->buf, sizeof(buffer->buf) - 1, 0, (struct sockaddr *)&remote_addr, (socklen_t *)&slen); | ||
} while (len < 0 && socket_eintr()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is attempting to read until recvfrom
stops failing? recvfrom returns a positive number representing how many bytes were read, or a negative number on failure, and 0 on "no messages available".
I find the way this is setup to be really weird, but maybe that's just the design and i'm not familiar enough with the project to understand yet.
|
||
if (len >= 0) { | ||
listen->received += len; | ||
if (listen->verbose) { | ||
uint8_t ra[64] = {0}, la[64] = {0}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
magic numbers. Please use a named constant or some kind of sizeof() calculation.
Also please don't declare multiple variables on the same line, it makes it impossible to understand what's going on
do { | ||
len = sendto(fd, buffer.buf, buffer.len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen); | ||
len = sendto(fd, buffer->buf, buffer->len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe i'm not understanding the design properly, but why are we sending the buffer we just read...? Doesn't the buffer need to be checked for contents of any sort?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless this particular application is simply intended to spit back the exact buffer it received.
If so, that doesn't seem all that useful, but i could use it in specific test situations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This app is an implementation of "peer" so that tests could be run without relying on external code/clients. Echoing back the received packets is what it does - for tests exactly @jonesmz
It's just a simple echo.
Statistics will be added later.
At 2024-01-20 04:59:28, "Michael Jones" ***@***.***> wrote:
@jonesmz commented on this pull request.
In src/apps/peer/udpserver.c:
do {
- len = sendto(fd, buffer.buf, buffer.len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen);
+ len = sendto(fd, buffer->buf, buffer->len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen);
Maybe i'm not understanding the design properly, but why are we sending the buffer we just read...? Doesn't the buffer need to be checked for contents of any sort?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|