Skip to content

Commit

Permalink
fix(*) fix typos and add error check for new_of/dup_of (fffonion#2)
Browse files Browse the repository at this point in the history
* format_error and format_all_error are not correctly referenced

* add error check for new_of/dup_of

* fix istype/stack_of
  • Loading branch information
catbro666 authored Jun 7, 2023
1 parent fb264ce commit 8bacfea
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/resty/openssl/pkcs12.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ local function decode(p12, passphrase)
local px509 = ptr_ptr_of_x509()
local pstack = ptr_ptr_of_stack()
local stack = stack_of_x509_new()
if stack == nil then
return nil, "pkcs12.decode: OPENSSL_sk_new_null() failed"
end

-- assign a valid OPENSSL_STACK so gc is taken care of
pstack[0] = stack

Expand Down Expand Up @@ -165,4 +169,4 @@ return {
loads = decode,
encode = encode,
dumps = encode,
}
}
5 changes: 4 additions & 1 deletion lib/resty/openssl/x509/altname.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ function _M.dup(ctx)
return nil, "x509.altname.dup: expect a GENERAL_NAMES* ctx at #1"
end

local dup_ctx = dup(ctx)
local dup_ctx, err = dup(ctx)
if dup_ctx == nil then
return nil, err
end

return setmetatable({
cast = ffi_cast("GENERAL_NAMES*", dup_ctx),
Expand Down
5 changes: 4 additions & 1 deletion lib/resty/openssl/x509/chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ local add = stack_lib.add_of(STACK)
local mt = stack_lib.mt_of(STACK, x509_lib.dup, _M)

function _M.new()
local raw = new()
local raw, err = new()
if raw == nil then
return nil, err
end

local self = setmetatable({
stack_of = STACK,
Expand Down
10 changes: 8 additions & 2 deletions lib/resty/openssl/x509/extension/dist_points.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function _M.new()
end

local self = setmetatable({
stack_of = STACK,
ctx = ctx,
_is_shallow_copy = false,
}, mt)
Expand All @@ -35,16 +36,21 @@ function _M.new()
end

function _M.istype(l)
return l and l.cast and ffi.istype(stack_ptr_ct, l.cast)
return l and l.ctx and ffi.istype(stack_ptr_ct, l.ctx)
and l.stack_of and l.stack_of == STACK
end

function _M.dup(ctx)
if ctx == nil or not ffi.istype(stack_ptr_ct, ctx) then
return nil, "expect a stack ctx at #1"
end
local dup_ctx = dup(ctx)
local dup_ctx, err = dup(ctx)
if dup_ctx == nil then
return nil, err
end

return setmetatable({
stack_of = STACK,
ctx = dup_ctx,
-- don't let lua gc the original stack to keep its elements
_dupped_from = ctx,
Expand Down
5 changes: 4 additions & 1 deletion lib/resty/openssl/x509/extension/info_access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ function _M.dup(ctx)
if ctx == nil or not ffi.istype(authority_info_access_ptr_ct, ctx) then
return nil, "expect a AUTHORITY_INFO_ACCESS* ctx at #1"
end
local dup_ctx = dup(ctx)
local dup_ctx, err = dup(ctx)
if dup_ctx == nil then
return nil, err
end

return setmetatable({
ctx = dup_ctx,
Expand Down
11 changes: 9 additions & 2 deletions lib/resty/openssl/x509/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ local dup = stack_lib.dup_of(STACK)
local mt = stack_lib.mt_of(STACK, extension_lib.dup, _M)

function _M.new()
local raw = new()
local raw, err = new()
if raw == nil then
return nil, err
end

local self = setmetatable({
stack_of = STACK,
Expand All @@ -37,9 +40,13 @@ function _M.dup(ctx)
return nil, "x509.extensions.dup: expect a stack ctx at #1, got " .. type(ctx)
end

local dup_ctx = dup(ctx)
local dup_ctx, err = dup(ctx)
if dup_ctx == nil then
return nil, err
end

return setmetatable({
stack_of = STACK,
ctx = dup_ctx,
-- don't let lua gc the original stack to keep its elements
_dupped_from = ctx,
Expand Down
4 changes: 2 additions & 2 deletions lib/resty/openssl/x509/store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ local x509_lib = require "resty.openssl.x509"
local chain_lib = require "resty.openssl.x509.chain"
local crl_lib = require "resty.openssl.x509.crl"
local ctx_lib = require "resty.openssl.ctx"
local format_error = require("resty.openssl.err").format_all_error
local format_all_error = require("resty.openssl.err").format_error
local format_all_error = require("resty.openssl.err").format_all_error
local format_error = require("resty.openssl.err").format_error
local OPENSSL_3X = require("resty.openssl.version").OPENSSL_3X

local _M = {}
Expand Down

0 comments on commit 8bacfea

Please sign in to comment.