From: Willy Tarreau Date: Mon, 6 Apr 2015 08:59:20 +0000 (+0200) Subject: CLEANUP: lua: remove hard-coded sizeof() in object creations and mallocs X-Git-Tag: v1.6-dev2~285 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07081fe6b70a9448dc91179ab76499e183c1d459;p=thirdparty%2Fhaproxy.git CLEANUP: lua: remove hard-coded sizeof() in object creations and mallocs Last bug was an example of a side effect of abuse of copy-paste, but there are other places at risk, so better fix all occurrences of sizeof to really reference the object size in order to limit the risks in the future. --- diff --git a/src/hlua.c b/src/hlua.c index d764fa55f5..ad2c22ff9d 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -2709,7 +2709,7 @@ static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe) * transaction object. */ lua_newtable(L); - hs = lua_newuserdata(L, sizeof(struct hlua_smp)); + hs = lua_newuserdata(L, sizeof(*hs)); lua_rawseti(L, -2, 0); hs->s = txn->s; @@ -2813,7 +2813,7 @@ static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsaf * same than the TXN object. */ lua_newtable(L); - hs = lua_newuserdata(L, sizeof(struct hlua_smp)); + hs = lua_newuserdata(L, sizeof(*hs)); lua_rawseti(L, -2, 0); hs->s = txn->s; @@ -2932,7 +2932,7 @@ static int hlua_http_new(lua_State *L, struct hlua_txn *txn) * same than the TXN object. */ lua_newtable(L); - ht = lua_newuserdata(L, sizeof(struct hlua_txn)); + ht = lua_newuserdata(L, sizeof(*ht)); lua_rawseti(L, -2, 0); ht->s = txn->s; @@ -3330,7 +3330,7 @@ static int hlua_txn_new(lua_State *L, struct session *s, struct proxy *p, void * */ /* Create the object: obj[0] = userdata. */ lua_newtable(L); - hs = lua_newuserdata(L, sizeof(struct hlua_txn)); + hs = lua_newuserdata(L, sizeof(*hs)); lua_rawseti(L, -2, 0); hs->s = s; @@ -3513,7 +3513,7 @@ __LJMP static int hlua_txn_set_mark(lua_State *L) mark = MAY_LJMP(luaL_checkinteger(L, 2)); if ((cli_conn = objt_conn(htxn->s->si[0].end)) && conn_ctrl_ready(cli_conn)) - setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(int)); + setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); #endif return 0; } @@ -4033,8 +4033,7 @@ __LJMP static int hlua_register_converters(lua_State *L) ref = MAY_LJMP(hlua_checkfunction(L, 2)); /* Allocate and fill the sample fetch keyword struct. */ - sck = malloc(sizeof(struct sample_conv_kw_list) + - sizeof(struct sample_conv) * 2); + sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2); if (!sck) WILL_LJMP(luaL_error(L, "lua out of memory error.")); fcn = malloc(sizeof(*fcn)); @@ -4094,8 +4093,7 @@ __LJMP static int hlua_register_fetches(lua_State *L) ref = MAY_LJMP(hlua_checkfunction(L, 2)); /* Allocate and fill the sample fetch keyword struct. */ - sfk = malloc(sizeof(struct sample_fetch_kw_list) + - sizeof(struct sample_fetch) * 2); + sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2); if (!sfk) WILL_LJMP(luaL_error(L, "lua out of memory error.")); fcn = malloc(sizeof(*fcn));