Skip to content

Commit

Permalink
Check that CFStringCreateWithCString succeeded
Browse files Browse the repository at this point in the history
Addresses #9
  • Loading branch information
jakewilliami committed Aug 29, 2022
1 parent 750cca4 commit ddcbd9b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/utils/darwin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ const K_CF_STRING_ENCODING_UTF8 = UInt32(65001)

# https://developer.apple.com/documentation/corefoundation/1542942-cfstringcreatewithcstring
function _cfstring_create_with_cstring(s::AbstractString, encoding::Unsigned = K_CFSTRING_ENCODING_MACROMAN)
return ccall(:CFStringCreateWithCString, Cstring,
(Ptr{Cvoid}, Cstring, UInt32),
C_NULL, s, encoding)
# TODO: check if result is null (if so, there was a problem creating the string)
cfstr = ccall(:CFStringCreateWithCString, Cstring,
(Ptr{Cvoid}, Cstring, UInt32),
C_NULL, s, encoding)
cfstr == C_NULL && error("Cannot create CF String for $(repr(s)) using encoding $(repr(encoding))")
return cfstr
end

# https://developer.apple.com/documentation/coreservices/1426917-mditemcreate
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using Test
f = String(rand(Char, 32)) # this path shouldn't exist
cfstr = HiddenFiles._cfstring_create_with_cstring(f)
@test_throws Exception HiddenFiles._mditem_create(cfstr)
@test_throws Exception HiddenFiles._cfstring_create_with_cstring("Julia", 0x1c000101) # this encoding mode should not exist
end
else
@testset "HiddenFiles.jl—UNIX excluding macOS" begin
Expand Down

0 comments on commit ddcbd9b

Please sign in to comment.