Mercurial > prosody-hg
comparison util-src/crypto.c @ 14179:25beb66fab38 13.0
util.crypto: Use post-Lua 5.1 buffer API
Tha Lua 5.1 buffer API was frustrating to work with. luaL_prepbuffer() only
allocates LUAL_BUFFERSIZE - there is no way to request a specific size.
LUAL_BUFFERSIZE is set to BUFSIZ by default, which is generally 8192 bytes on
Linux, however it may vary between platforms.
The variation means this buffer could potentially overflow (small BUFSIZ with
large signature).
The Lua 5.2+ buffer API enables requesting a specific buffer size, so that's
what we do now.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 25 May 2026 15:11:47 +0100 |
| parents | af87d31dcae4 |
| children | d975d280baec |
comparison
equal
deleted
inserted
replaced
| 14178:af87d31dcae4 | 14179:25beb66fab38 |
|---|---|
| 148 if(EVP_DigestSign(md_ctx, NULL, &sig_len, msg, msg_len) != 1) { | 148 if(EVP_DigestSign(md_ctx, NULL, &sig_len, msg, msg_len) != 1) { |
| 149 lua_pushnil(L); | 149 lua_pushnil(L); |
| 150 return 1; | 150 return 1; |
| 151 } | 151 } |
| 152 | 152 |
| 153 // COMPAT w/ Lua 5.1 | 153 sig = (unsigned char *)luaL_buffinitsize(L, &sigbuf, sig_len); |
| 154 luaL_buffinit(L, &sigbuf); | |
| 155 sig = memset(luaL_prepbuffer(&sigbuf), 0, sig_len); | |
| 156 | 154 |
| 157 if(EVP_DigestSign(md_ctx, sig, &sig_len, msg, msg_len) != 1) { | 155 if(EVP_DigestSign(md_ctx, sig, &sig_len, msg, msg_len) != 1) { |
| 158 lua_pushnil(L); | 156 lua_pushnil(L); |
| 159 } | 157 } |
| 160 else { | 158 else { |
| 161 luaL_addsize(&sigbuf, sig_len); | 159 luaL_pushresultsize(&sigbuf, sig_len); |
| 162 luaL_pushresult(&sigbuf); | |
| 163 return 1; | 160 return 1; |
| 164 } | 161 } |
| 165 | 162 |
| 166 return 1; | 163 return 1; |
| 167 } | 164 } |
