convert always has exit code 0 (success) even if failed

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
fraz
Posts: 7
Joined: 2018-04-01T05:44:25-07:00
Authentication code: 1152

convert always has exit code 0 (success) even if failed

Post by fraz »

I'm writing a script to recursively test JPGs for corruption using imagemagick to open and resize the image.
And on testing the error codes returned on a known bad jpg I always get a "0" success.

convert -resize 100 BNE4_0288.jpg /tmp/out.jpg
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST5 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST6 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST7 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST0 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: found marker 0xea instead of RST1 `BNE4_0288.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
ETC!!
echo $?
gives me a 0.

if I check out.jpg it's truncated and mostly grey. Sure it was made, but I wouldn't call that a '0'.

Can this be assigned a non-zero error code?
fraz
Posts: 7
Joined: 2018-04-01T05:44:25-07:00
Authentication code: 1152

Re: convert always has exit code 0 (success) even if failed

Post by fraz »

Here's the example images:
https://www.dropbox.com/sh/bg3ht5k62ml0 ... 8qqTa?dl=0

The corruption seems to be random bytes/blocks corrupted by a bad USB flash drive.

I've also just tested it with a truncated image made using dd count=xxx and it also fails:
convert-im6.q16: Premature end of JPEG file `qqq.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
convert-im6.q16: Corrupt JPEG data: premature end of data segment `qqq.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
But echo $? returns 0 (success).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert always has exit code 0 (success) even if failed

Post by snibgo »

Those are warnings. If you want to treat warnings as errors, see http://www.imagemagick.org/script/comma ... d-warnings
snibgo's IM pages: im.snibgo.com
fraz
Posts: 7
Joined: 2018-04-01T05:44:25-07:00
Authentication code: 1152

Re: convert always has exit code 0 (success) even if failed

Post by fraz »

Ah, thanks.
That makes it work as expected.
convert -regard-warnings -resize 100 qqq.jpg out.jpg
Echo $? gives me a 1.

I would have thought this would be a more sensible default and -disregard-warnings should be the option.
Feel free to move this out of the bug section though.

Thanks again!
fraz
Posts: 7
Joined: 2018-04-01T05:44:25-07:00
Authentication code: 1152

Re: convert always has exit code 0 (success) even if failed

Post by fraz »

And for anyone looking for the final script. Here it is:

Code: Select all

#!/bin/bash

newline='
'

OIFS=$IFS
IFS=$newline
files=($(find . -iname "*.$1"))
IFS=$OIFS

for i in "${files[@]}"
do

echo -n "Testing: $i.."
convert -regard-warnings -resize 100 "$i" /dev/null
error=$?

if [ $error -ne 0 ]
then
  echo "this didn't work!!"
  echo "$i"
  echo "$i" >> ~/brokenJPGs.txt
  file "$i" >> ~/brokenFILE.txt
else
  echo "OK"

fi

done

wc ~/brokenJPGs.txt

exit 0
Make sure you pass the extension to test as the first argument. It produces a file called brokenJPGs.txt as a file list for further processing. And brokenFILE.txt which has the 'file' output to find misnamed things etc.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert always has exit code 0 (success) even if failed

Post by snibgo »

IM raises an error when it can't read an image, or can't create an output image, and so on. If there is a problem with data or metadata, but IM can still process the image, it raises a warning.

Personally, I would like an extra level, so we might have:
3: can't process the image
2: can process the image, but it is probably wrong (eg "corrupt JPEG data")
1: can process the image, but it's probably okay (eg "ASCII value for tag "Make" contains null byte in value")
0: no problem at all
snibgo's IM pages: im.snibgo.com
Post Reply