Skip to content

Commit e968ca2

Browse files
Fixed overallocation of memory (#232)
* Fixed overallocation of memory * Fix overallocation of memory without modifying function definition
1 parent 8606de6 commit e968ca2

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/context.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ struct us_listen_socket_t *us_socket_context_listen(int ssl, struct us_socket_co
263263
return 0;
264264
}
265265

266-
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t));
266+
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t) - sizeof(struct us_poll_t));
267267
us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET);
268268
us_poll_start(p, context->loop, LIBUS_SOCKET_READABLE);
269269

@@ -294,7 +294,7 @@ struct us_listen_socket_t *us_socket_context_listen_unix(int ssl, struct us_sock
294294
return 0;
295295
}
296296

297-
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t));
297+
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t) - sizeof(struct us_poll_t));
298298
us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET);
299299
us_poll_start(p, context->loop, LIBUS_SOCKET_READABLE);
300300

@@ -325,7 +325,7 @@ struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_
325325
}
326326

327327
/* Connect sockets are semi-sockets just like listen sockets */
328-
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) + socket_ext_size);
328+
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) - sizeof(struct us_poll_t) + socket_ext_size);
329329
us_poll_init(p, connect_socket_fd, POLL_TYPE_SEMI_SOCKET);
330330
us_poll_start(p, context->loop, LIBUS_SOCKET_WRITABLE);
331331

@@ -354,7 +354,7 @@ struct us_socket_t *us_socket_context_connect_unix(int ssl, struct us_socket_con
354354
}
355355

356356
/* Connect sockets are semi-sockets just like listen sockets */
357-
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) + socket_ext_size);
357+
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) - sizeof(struct us_poll_t) + socket_ext_size);
358358
us_poll_init(p, connect_socket_fd, POLL_TYPE_SEMI_SOCKET);
359359
us_poll_start(p, context->loop, LIBUS_SOCKET_WRITABLE);
360360

src/crypto/openssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ void *us_internal_ssl_socket_context_get_native_handle(struct us_internal_ssl_so
407407
struct us_internal_ssl_socket_context_t *us_internal_create_child_ssl_socket_context(struct us_internal_ssl_socket_context_t *context, int context_ext_size) {
408408
/* Create a new non-SSL context */
409409
struct us_socket_context_options_t options = {0};
410-
struct us_internal_ssl_socket_context_t *child_context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, context->sc.loop, sizeof(struct us_internal_ssl_socket_context_t) + context_ext_size, options);
410+
struct us_internal_ssl_socket_context_t *child_context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, context->sc.loop, sizeof(struct us_internal_ssl_socket_context_t) - sizeof(struct us_socket_context_t) + context_ext_size, options);
411411

412412
/* The only thing we share is SSL_CTX */
413413
child_context->ssl_context = context->ssl_context;
@@ -645,7 +645,7 @@ struct us_internal_ssl_socket_context_t *us_internal_create_ssl_socket_context(s
645645
}
646646

647647
/* Otherwise ee continue by creating a non-SSL context, but with larger ext to hold our SSL stuff */
648-
struct us_internal_ssl_socket_context_t *context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, loop, sizeof(struct us_internal_ssl_socket_context_t) + context_ext_size, options);
648+
struct us_internal_ssl_socket_context_t *context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, loop, sizeof(struct us_internal_ssl_socket_context_t) - sizeof(struct us_socket_context_t) + context_ext_size, options);
649649

650650
/* I guess this is the only optional callback */
651651
context->on_server_name = NULL;

src/eventing/epoll_kqueue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ unsigned int us_internal_accept_poll_event(struct us_poll_t *p) {
287287
/* Timer */
288288
#ifdef LIBUS_USE_EPOLL
289289
struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
290-
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) + ext_size);
290+
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) - sizeof(struct us_poll_t) + ext_size);
291291
int timerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
292292
if (timerfd == -1) {
293293
return NULL;
@@ -372,7 +372,7 @@ void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms
372372
/* Async (internal helper for loop's wakeup feature) */
373373
#ifdef LIBUS_USE_EPOLL
374374
struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
375-
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) + ext_size);
375+
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) - sizeof(struct us_poll_t) + ext_size);
376376
us_poll_init(p, eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC), POLL_TYPE_CALLBACK);
377377

378378
struct us_internal_callback_t *cb = (struct us_internal_callback_t *) p;

src/udp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, struct us_u
118118
int ext_size = 0;
119119
int fallthrough = 0;
120120

121-
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_udp_t) + ext_size);
121+
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_udp_t) - sizeof(struct us_poll_t) + ext_size);
122122
us_poll_init(p, fd, POLL_TYPE_CALLBACK);
123123

124124
struct us_internal_udp_t *cb = (struct us_internal_udp_t *) p;

0 commit comments

Comments
 (0)