Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions kafka/producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,19 @@ lua_producer_produce(struct lua_State *L) {

lua_pushstring(L, "key");
lua_gettable(L, -2 );
size_t key_len;
// rd_kafka will copy key so no need to worry about this cast
char *key = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
char *key = (char *)lua_tolstring(L, -1, &key_len);

size_t key_len = key != NULL ? strlen(key) : 0;
lua_pop(L, 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as we discussed in Tarantool-ru telegram chat, this approach could lead to use-after-free (https://pgl.yoyo.org/luai/i/lua_tolstring)

Maybe we should use luaL_ref/luaL_unref here.

Also it would be great to add a test for such case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem of use after free was present before (and I suppose this is another issue), this PR just replace strlen to lua_tolstring

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you are right. I don't have objections.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach could lead to use-after-free

@filonenko-mikhail @olegrok please confirm or deny that this issue has been fixed till the current day's release.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to problem still exists


lua_pushstring(L, "value");
lua_gettable(L, -2 );
size_t value_len;
// rd_kafka will copy value so no need to worry about this cast
char *value = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
char *value = (char *)lua_tolstring(L, -1, &value_len);

size_t value_len = value != NULL ? strlen(value) : 0;
lua_pop(L, 1);

if (key == NULL && value == NULL) {
int fail = safe_pushstring(L, "producer message must contains non nil key or value");
Expand Down