Avoid malloc/free for vars of known sizes

pull/13/head
Soner Tari 5 years ago
parent d0ad45e74d
commit 44b125f77e

@ -583,7 +583,7 @@ privsep_server_handle_req(opts_t *opts, int srvsock)
break;
}
case PRIVSEP_REQ_UPDATE_ATIME: {
userdbkeys_t *arg;
userdbkeys_t arg;
if (n != sizeof(char) + sizeof(userdbkeys_t)) {
ans[0] = PRIVSEP_ANS_INVALID;
@ -594,24 +594,8 @@ privsep_server_handle_req(opts_t *opts, int srvsock)
}
return 0;
}
// @attention Do not typecast, but malloc and memcpy
//arg = *(userdbkeys_t**)(&req[1]);
if (!(arg = malloc(n))) {
ans[0] = PRIVSEP_ANS_SYS_ERR;
*((int*)&ans[1]) = errno;
if (sys_sendmsgfd(srvsock, ans, 1 + sizeof(int),
-1) == -1) {
log_err_level_printf(LOG_CRIT, "Sending message failed: %s (%i"
")\n", strerror(errno), errno);
return -1;
}
return 0;
}
memcpy(arg, req + 1, n - 1);
if (privsep_server_update_atime(opts, arg) == -1) {
free(arg);
arg = *(userdbkeys_t*)(&req[1]);
if (privsep_server_update_atime(opts, &arg) == -1) {
ans[0] = PRIVSEP_ANS_SYS_ERR;
*((int*)&ans[1]) = errno;
if (sys_sendmsgfd(srvsock, ans, 1 + sizeof(int),
@ -622,7 +606,6 @@ privsep_server_handle_req(opts_t *opts, int srvsock)
}
return 0;
} else {
free(arg);
ans[0] = PRIVSEP_ANS_SUCCESS;
// @attention Pass -1 as arg 4, otherwise passing 0 opens an stdin (fd 0), causing fd leak
if (sys_sendmsgfd(srvsock, ans, 1, -1) == -1) {

@ -391,25 +391,22 @@ pxy_conn_ctx_free(pxy_conn_ctx_t *ctx, int by_requestor)
// Update userdb atime if idle time is more than 50% of user timeout, which is expected to reduce update frequency
unsigned int idletime = ctx->idletime + (time(NULL) - ctx->ctime);
if (idletime > (ctx->opts->user_timeout / 2)) {
struct userdbkeys *keys = malloc(sizeof(userdbkeys_t));
if (keys) {
// Zero out for NULL termination
memset(keys, 0, sizeof(userdbkeys_t));
// Leave room for NULL to make sure the strings are always NULL terminated
strncpy(keys->ip, ctx->srchost_str, sizeof(keys->ip) - 1);
strncpy(keys->user, ctx->user, sizeof(keys->user) - 1);
strncpy(keys->ether, ctx->ether, sizeof(keys->ether) - 1);
if (privsep_client_update_atime(ctx->clisock, keys) == -1) {
userdbkeys_t keys;
// Zero out for NULL termination
memset(&keys, 0, sizeof(userdbkeys_t));
// Leave room for NULL to make sure the strings are always NULL terminated
strncpy(keys.ip, ctx->srchost_str, sizeof(keys.ip) - 1);
strncpy(keys.user, ctx->user, sizeof(keys.user) - 1);
strncpy(keys.ether, ctx->ether, sizeof(keys.ether) - 1);
if (privsep_client_update_atime(ctx->clisock, &keys) == -1) {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Error updating user atime: %s, ctx->fd=%d\n", sqlite3_errmsg(ctx->opts->userdb), ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Error updating user atime: %s, ctx->fd=%d\n", sqlite3_errmsg(ctx->opts->userdb), ctx->fd);
#endif /* DEBUG_PROXY */
} else {
} else {
#ifdef DEBUG_PROXY
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Successfully updated user atime, ctx->fd=%d\n", ctx->fd);
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "pxy_conn_ctx_free: Successfully updated user atime, ctx->fd=%d\n", ctx->fd);
#endif /* DEBUG_PROXY */
}
free(keys);
}
} else {
#ifdef DEBUG_PROXY

Loading…
Cancel
Save