Skip to content

Commit

Permalink
Fix windows fdopen error (refs #229)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfx committed Jul 16, 2023
1 parent 7d68f0a commit 12339d3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

jobs:
test:
name: Test ImageMagick6
name: Test ImageMagick6 linux
runs-on: ubuntu-latest
environment: imagemagick6
env:
Expand Down
10 changes: 8 additions & 2 deletions imagick/magick_wand_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3380,11 +3380,17 @@ func (mw *MagickWand) WriteImagesFile(out *os.File) error {
// cfdopen returns a C-level FILE*. mode should be as described in fdopen(3).
// Caller is responsible for closing the file when successfully returned,
// via C.fclose()
func cfdopen(file *os.File, mode string) (*C.FILE, error) {
func cfdopen(file *os.File, mode string) (cfile *C.FILE, err error) {
cname := C.CString(file.Name())
cmode := C.CString(mode)
defer C.free(unsafe.Pointer(cname))
defer C.free(unsafe.Pointer(cmode))

cfile, err := C.fdopen(C.dup(C.int(file.Fd())), cmode)
if file.Name() != "" {
cfile, err = C.fopen(cname, cmode)
} else {
cfile, err = C.fdopen(C.dup(C.int(file.Fd())), cmode)
}
if err != nil {
return nil, err
}
Expand Down
29 changes: 29 additions & 0 deletions imagick/magick_wand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package imagick

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"runtime"
"sync/atomic"
Expand Down Expand Up @@ -127,6 +129,33 @@ func TestDeleteImageArtifact(t *testing.T) {
}
}

func TestReadImageFile(t *testing.T) {
Initialize()
defer Terminate()

mw := NewMagickWand()
if err := mw.ReadImage(`logo:`); err != nil {
t.Fatal(err)
}

tmp, err := ioutil.TempFile("", "imagick_test-*.jpg")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())

if err := mw.WriteImage(tmp.Name()); err != nil {
t.Fatal(err)
}
mw.Destroy()

mw = NewMagickWand()
defer mw.Destroy()
if err := mw.ReadImageFile(tmp); err != nil {
t.Fatal(err)
}
}

func TestReadImageBlob(t *testing.T) {
Initialize()
defer func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions imagick/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func relinquishMemory(ptr unsafe.Pointer) {

// relinquishes memory resources, null terminated array of strings
func relinquishMemoryCStringArray(p **C.char) {
if p == nil {
return
}
defer relinquishMemory(unsafe.Pointer(p))
for *p != nil {
relinquishMemory(unsafe.Pointer(*p))
Expand Down

0 comments on commit 12339d3

Please sign in to comment.