# Buggy code
def extract_image(args):
    """The image can either be downloaded from a container registry or provided
    as an image tarball. Extract the image into a working directory accordingly
    Return an image name and tag and an image digest if it exists"""
    if args.docker_image:
        # extract the docker image
        image_attrs = docker_api.dump_docker_image(args.docker_image)
        if image_attrs:
            # repo name and digest is preferred, but if that doesn't exist
            # the repo name and tag will do
            if image_attrs["RepoDigests"]:
                image_string = image_attrs["RepoDigests"][0]
            if image_attrs["RepoTags"]:
                image_string = image_attrs["RepoTags"][0]
            return image_string
        logger.critical("Cannot extract Docker image")
    if args.raw_image:
        # for now we assume that the raw image tarball is always
        # the product of "docker save", hence it will be in
        # the docker style layout
        if rootfs.extract_tarfile(args.raw_image, rootfs.get_working_dir()):
            return args.raw_image
        logger.critical("Cannot extract raw image")
    return None


# Traceback (exception)
{ memoryInfo:
'{"rss":39120896,"heapTotal":9961472,"heapUsed":5357888,"external":8962}',
errorCode: 1,
errorSignal: null,
errorMessage:
'Traceback (most recent call last):\n  File "/usr/local/bin/tern", line 8, in <module>\n    sys.exit(main())\n  File "/usr/local/lib/python3.7/dist-packages/tern/__main__.py", line 192, in main\n    do_main(args)\n  File "/usr/local/lib/python3.7/dist-packages/tern/__main__.py", line 95, in do_main\n    crun.execute_image(args)\n  File "/usr/local/lib/python3.7/dist-packages/tern/analyze/default/container/run.py", line 73, in execute_image\n    image_string = extract_image(args)\n  File "/usr/local/lib/python3.7/dist-packages/tern/analyze/default/container/run.py", line 40, in extract_image\n    return image_string\nUnboundLocalError: local variable \'image_string\' referenced before assignment\n',
outputString: '',
exception: undefined }

# Fixed code
def extract_image(args):
    """The image can either be downloaded from a container registry or provided
    as an image tarball. Extract the image into a working directory accordingly
    Return an image name and tag and an image digest if it exists"""
    if args.docker_image:
        # extract the docker image
        image_attrs = docker_api.dump_docker_image(args.docker_image)
        if image_attrs:
            # repo name and digest is preferred, but if that doesn't exist
            # the repo name and tag will do. If neither exist use repo Id.
            if image_attrs["Id"]:
                image_string = image_attrs["Id"]
            if image_attrs["RepoTags"]:
                image_string = image_attrs["RepoTags"][0]
            if image_attrs["RepoDigests"]:
                image_string = image_attrs["RepoDigests"][0]
            return image_string
        logger.critical("Cannot extract Docker image")
    if args.raw_image:
        # for now we assume that the raw image tarball is always
        # the product of "docker save", hence it will be in
        # the docker style layout
        if rootfs.extract_tarfile(args.raw_image, rootfs.get_working_dir()):
            return args.raw_image
        logger.critical("Cannot extract raw image")
    return None