Skip to content

Commit 57c19ca

Browse files
committed
Reduce scope of try-statements.
The only unknown is the users function, any other exceptions are bugs in SQLite.jl and should be fixed not hidden away.
1 parent fa7742d commit 57c19ca

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/UDF.jl

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,37 +102,33 @@ function stepfunc(init, func, fsym=symbol(string(func)*"_step"))
102102
acval = sqldeserialize(acvalbuf)
103103
end
104104

105+
local funcret
105106
try
106107
funcret = sqlserialize($(func)(acval, args...))
107-
108-
newsize = sizeof(funcret)
109-
if newsize > valsize
110-
# TODO: increase this in a cleverer way?
111-
valptr = convert(Ptr{UInt8}, c_realloc(valptr, newsize))
112-
end
113-
# copy serialized return value
114-
unsafe_copy!(valptr, pointer(funcret), newsize)
115-
116-
# copy the size of the serialized value
117-
unsafe_copy!(
118-
acptr,
119-
pointer(reinterpret(UInt8, [newsize])),
120-
intsize
121-
)
122-
# copy the address of the pointer to the serialized value
123-
valarr = reinterpret(UInt8, [valptr])
124-
for i in 1:length(valarr)
125-
unsafe_store!(acptr, valarr[i], intsize+i)
126-
end
127108
catch
128-
# TODO:
129-
# this won't catch all memory leaks so add an else clause
130-
# alternatively use c-style checking in this function
131-
if isdefined(:valptr)
132-
c_free(valptr)
133-
end
109+
c_free(valptr)
134110
rethrow()
135111
end
112+
113+
newsize = sizeof(funcret)
114+
if newsize > valsize
115+
# TODO: increase this in a cleverer way?
116+
valptr = convert(Ptr{UInt8}, c_realloc(valptr, newsize))
117+
end
118+
# copy serialized return value
119+
unsafe_copy!(valptr, pointer(funcret), newsize)
120+
121+
# copy the size of the serialized value
122+
unsafe_copy!(
123+
acptr,
124+
pointer(reinterpret(UInt8, [newsize])),
125+
intsize
126+
)
127+
# copy the address of the pointer to the serialized value
128+
valarr = reinterpret(UInt8, [valptr])
129+
for i in 1:length(valarr)
130+
unsafe_store!(acptr, valarr[i], intsize+i)
131+
end
136132
nothing
137133
end
138134
end
@@ -159,17 +155,18 @@ function finalfunc(init, func, fsym=symbol(string(func)*"_final"))
159155
Ptr{UInt8}, bytestoint(acptr, intsize+1, ptrsize)
160156
)
161157

162-
try
163-
# load value
164-
acvalbuf = zeros(UInt8, valsize)
165-
unsafe_copy!(pointer(acvalbuf), valptr, valsize)
166-
acval = sqldeserialize(acvalbuf)
158+
# load value
159+
acvalbuf = zeros(UInt8, valsize)
160+
unsafe_copy!(pointer(acvalbuf), valptr, valsize)
161+
acval = sqldeserialize(acvalbuf)
167162

163+
local ret
164+
try
168165
ret = $(func)(acval)
169-
sqlreturn(context, ret)
170166
finally
171167
c_free(valptr)
172168
end
169+
sqlreturn(context, ret)
173170
end
174171
nothing
175172
end

0 commit comments

Comments
 (0)