Some fixing. Also allocate the buffer for checking image validity on the stack. We really don't need to call malloc for 8 bytes.