Spot the error.. need help converting a c libturbojpeg example

Found a workaround by pre-allocating the memory to the maximum possible size, as per the 1st option in the docs libjpeg.turbojpeg source code

    jpegSize = tjBufSize(width, height, jpegSubsamp)
    jpegBuf = tjAlloc(jpegSize)

It’s not ideal as it requires the allocation to be the max possible, which is actually larger than the source image size…