diff options
| author | amelia squires <[email protected]> | 2024-09-16 12:16:29 -0500 |
|---|---|---|
| committer | amelia squires <[email protected]> | 2024-09-16 12:16:29 -0500 |
| commit | 1feb3c40514a7c1726f29502ce37966c308bea79 (patch) | |
| tree | d1a5a26af92af8fabf12d5095a2d3745431593b4 | |
| parent | dbf438f04dac63f2a32743fded1b7fbf02ed636b (diff) | |
fix _copy and mem leaks
| -rw-r--r-- | src/crypto.h | 7 | ||||
| -rw-r--r-- | src/hash/blake.c | 12 | ||||
| -rw-r--r-- | src/hash/md5.c | 9 | ||||
| -rw-r--r-- | src/hash/sha01.c | 17 | ||||
| -rw-r--r-- | src/hash/sha2-256.c | 20 | ||||
| -rw-r--r-- | src/hash/sha2xx.c | 22 | ||||
| -rw-r--r-- | tests/hash.lua | 22 | ||||
| -rw-r--r-- | tests/t.lua | 5 | ||||
| -rw-r--r-- | tests/thread.lua | 15 |
9 files changed, 112 insertions, 17 deletions
diff --git a/src/crypto.h b/src/crypto.h index b3b0609..ba97f0c 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -44,15 +44,16 @@ int tp(lua_State*); #define common_hash_init_update(hashname) lua_common_hash_init_update(hashname, hashname)
#define lua_common_hash_init_update(hashname, luaname) lua_common_hash_init(hashname, luaname) lua_common_hash_update(hashname, luaname)
#define lua_common_hash_init(hashname, luaname) lua_common_hash_init_ni(hashname, luaname, hashname##_init())
+#define lua_common_hash_init_l(hashname, luaname) lua_common_hash_init_ni(hashname, luaname, hashname##_init_l(L))
#define common_hash_clone(hashname) lua_common_hash_clone(hashname, hashname)
-#define lua_common_hash_clone(hashname, luaname) lua_common_hash_clone_oargs(hashname, luaname, l_##luaname##_init(L))
-#define lua_common_hash_clone_oargs(hashname, luaname, oinit)\
+#define lua_common_hash_clone(hashname, luaname) lua_common_hash_clone_oargs(hashname, luaname, l_##luaname##_init(L), *b = *a)
+#define lua_common_hash_clone_oargs(hashname, luaname, oinit, copy)\
int l_##luaname##_clone(lua_State* L){\
struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
oinit;\
struct hashname##_hash* b = (struct hashname##_hash*)lua_touserdata(L, -1);\
- *b = *a;\
+ copy;\
return 1;\
}
diff --git a/src/hash/blake.c b/src/hash/blake.c index 7bf1481..cf948f1 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -498,7 +498,8 @@ int l_blake256_clone(lua_State* L){ return 1;
}
-common_hash_init_update(blake256);
+lua_common_hash_init_l(blake256, blake256);
+lua_common_hash_update(blake256, blake256);
int l_blake256_final(lua_State* L){
struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, 1);
@@ -539,7 +540,8 @@ int l_blake224_clone(lua_State* L){ return 1;
}
-common_hash_init_update(blake224);
+lua_common_hash_init_l(blake224, blake224);
+lua_common_hash_update(blake224, blake224);
int l_blake224_final(lua_State* L){
struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, 1);
@@ -580,7 +582,8 @@ int l_blake512_clone(lua_State* L){ return 1;
}
-common_hash_init_update(blake512);
+lua_common_hash_init_l(blake512, blake512);
+lua_common_hash_update(blake512, blake512);
int l_blake512_final(lua_State* L){
struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, 1);
@@ -622,7 +625,8 @@ int l_blake384_clone(lua_State* L){ return 1;
}
-common_hash_init_update(blake384);
+lua_common_hash_init_l(blake384, blake384);
+lua_common_hash_update(blake384, blake384);
int l_blake384_final(lua_State* L){
struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, 1);
diff --git a/src/hash/md5.c b/src/hash/md5.c index 1b3f508..45844f2 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -134,7 +134,14 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){ }
-common_hash_clone(md5);
+//common_hash_clone(md5);
+lua_common_hash_clone_oargs(md5, md5, l_md5_init(L), {
+ uint8_t* old = b->buffer;
+ *b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+});
+
lua_common_hash_init_ni(md5, md5, md5_init_l(L));
lua_common_hash_update(md5, md5);
//common_hash_init_update(md5);
diff --git a/src/hash/sha01.c b/src/hash/sha01.c index 9ba3e6c..1711faa 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -174,11 +174,24 @@ void sha1(uint8_t* a, size_t len, char* out_stream){ free(aa.buffer);
}
-common_hash_clone(sha1);
+//common_hash_clone(sha1);
+lua_common_hash_clone_oargs(sha1, sha1, l_sha1_init(L), {
+ uint8_t* old = b->buffer;
+ *b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+});
+
lua_common_hash_init_ni(sha1, sha1, sha01_init_l(1, L));
lua_common_hash_update(sha1, sha1);
-common_hash_clone(sha0);
+//common_hash_clone(sha0);
+lua_common_hash_clone_oargs(sha0, sha0, l_sha0_init(L), {
+ uint8_t* old = b->buffer;
+ *b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+});
lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L));
lua_common_hash_update(sha0, sha0);
diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 7e37d9d..42f6a81 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -245,7 +245,13 @@ struct iv sha_iv_gen(int i){ return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7};
}
-common_hash_clone(sha512);
+//common_hash_clone(sha512);
+lua_common_hash_clone_oargs(sha512, sha512, l_sha512_init(L), {
+ uint8_t* old = b->buffer;
+ *b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+});
lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L));
lua_common_hash_update(sha512, sha512);
@@ -259,7 +265,13 @@ int l_sha512_final(lua_State* L){ return 1;
}
-common_hash_clone(sha384);
+//common_hash_clone(sha384);
+lua_common_hash_clone_oargs(sha384, sha384, l_sha384_init(L), {
+ uint8_t* old = b->buffer;
+ *b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+});
lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L));
lua_common_hash_update(sha384, sha384);
@@ -278,7 +290,11 @@ int l_sha512_t_clone(lua_State* L){ lua_pushinteger(L, a->t);
l_sha512_t_init(L);
struct sha512_hash* b = (struct sha512_hash*)lua_touserdata(L, -1);
+
+ uint8_t* old = b->buffer;
*b = *a;
+ b->buffer = old;
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
return 1;
}
diff --git a/src/hash/sha2xx.c b/src/hash/sha2xx.c index f3fbc5d..1ef3f6f 100644 --- a/src/hash/sha2xx.c +++ b/src/hash/sha2xx.c @@ -179,16 +179,24 @@ void sha256(uint8_t* in, size_t len, char* out){ struct sha256_hash a = sha256_init(); sha256_update(in, len, &a); sha256_final(&a, out); + free(a.buffer); } void sha224(uint8_t* in, size_t len, char* out){ struct sha256_hash a = sha224_init(); sha224_update(in, len, &a); sha224_final(&a, out); + free(a.buffer); } -common_hash_clone(sha256); -lua_common_hash_init(sha256, sha256); +//common_hash_clone(sha256); +lua_common_hash_clone_oargs(sha256, sha256, l_sha256_init(L), { + uint8_t* old = b->buffer; + *b = *a; + b->buffer = old; + memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); +}); +lua_common_hash_init_l(sha256, sha256); lua_common_hash_update(sha256, sha256); int l_sha256_final(lua_State* L){ @@ -215,8 +223,14 @@ int l_sha256(lua_State* L){ }; #define sha224_hash sha256_hash -common_hash_clone(sha224); -lua_common_hash_init(sha224, sha224); +//common_hash_clone(sha224); +lua_common_hash_clone_oargs(sha224, sha224, l_sha224_init(L), { + uint8_t* old = b->buffer; + *b = *a; + b->buffer = old; + memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); +}); +lua_common_hash_init_l(sha224, sha224); lua_common_hash_update(sha224, sha224); int l_sha224_final(lua_State* L){ diff --git a/tests/hash.lua b/tests/hash.lua index 60d33a8..d314167 100644 --- a/tests/hash.lua +++ b/tests/hash.lua @@ -1,5 +1,7 @@ llib = require "lullaby"
+--llib.io.log = function() end
+
local hashes_working = 0
local hashes_failed = 0
local functions_working = 0
@@ -14,6 +16,8 @@ function test(name,b,exp,oargs) local hash5
local hash6
local hash7
+ local hash8
+ local hash9
local add = ""
if oargs == nil then
hash = llib.crypto[name](b)
@@ -28,13 +32,29 @@ function test(name,b,exp,oargs) hash5 = llib.crypto[name]()
hash6 = hash5 + b;
hash6 = hash6:final()
- hash5 = hash5:update(b):final()
+ hash5 = hash5:update(b):final()
+ hash7 = llib.crypto[name]()
else
hash2 = llib.crypto[name](table.unpack(oargs)):update(b):final()
hash5 = llib.crypto[name](table.unpack(oargs))
hash6 = hash5 + b;
hash6 = hash6:final()
hash5 = hash5:update(b):final()
+ hash7 = llib.crypto[name](table.unpack(oargs))
+ end
+
+ hash8 = hash7 + "test"
+ hash9 = hash8:final()
+ hash7 = hash7:update("meo"):final()
+ hash8 = hash8:final()
+
+ if hash8 ~= hash9 then
+ fail = true
+ functions_failed = functions_failed + 1
+ llib.io.error(name.." __copy not working")
+ else
+ functions_working = functions_working + 1
+ llib.io.log(name.." __copy working")
end
if(hash5 ~= exp) then
diff --git a/tests/t.lua b/tests/t.lua new file mode 100644 index 0000000..9c71c61 --- /dev/null +++ b/tests/t.lua @@ -0,0 +1,5 @@ +a = require "lullaby" + +o = a.crypto.sha224() +--o:update("me") +--print(o:final()) diff --git a/tests/thread.lua b/tests/thread.lua new file mode 100644 index 0000000..8b7de88 --- /dev/null +++ b/tests/thread.lua @@ -0,0 +1,15 @@ +llby = require "lullaby" + +llby.thread.async(function(res, rej) + print("hi") + a = llby.crypto.sha512() + a:update("hi") + b = a + "meow" + print((b + "hi"):final()) + print((a:update("hi")):final()) + print((b + "hi"):final()) +end) + +os.execute("sleep 1") + + |
