tonhowtf/omniget · error

nao e um JPEG

Error message

nao e um JPEG

What it means

jpeg_info parses width/height/channels from a JPEG's SOF marker. It first checks that the data is at least 4 bytes and starts with the JPEG SOI signature FF D8; otherwise it raises "nao e um JPEG" (not a JPEG). It is a fast-fail guard so build_pdf never embeds non-JPEG bytes.

Solutions

  1. Check the first bytes (FF D8) or call is_jpeg() before invoking jpeg_info
  2. Re-download or re-obtain the file and verify it is a valid JPEG (file command / magic bytes)
  3. Convert other formats to JPEG first (e.g. with the image crate) if JPEG is required

Example fix

// before
let info = jpeg_info(&bytes)?; // Err: nao e um JPEG
// after
if !is_jpeg(&bytes) {
    let jpg = image::load_from_memory(&bytes)?.to_rgb8(); // convert/validate first
}
let info = jpeg_info(&bytes)?;
Defensive patterns

Strategy: type-guard

Validate before calling

if bytes.len() < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8 {
    return Err("entrada não é um JPEG válido");
}

Type guard

fn is_jpeg(data: &[u8]) -> bool {
    data.len() > 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF
}

Try / catch

match jpeg_info(&bytes) {
    Err(e) if e.to_string() == "nao e um JPEG" => {
        let jpg = convert_to_jpeg(&bytes)?;
        jpeg_info(&jpg)
    }
    r => r,
}

Prevention

When it happens

Trigger: Calling jpeg_info with an empty/short buffer, a PNG/WebP/HEIC file, a truncated download that lost the first bytes, or a text/HTML error page saved as .jpg.

Common situations: Downloads converted with content-negociation returning WebP/PNG; URL returned an HTML 404 page; file truncated by a failed download; user selected a non-JPEG image for the PDF builder.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/5f2cce70de7ca716. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/omniget-core/src/core/tools/jpeg_pdf.rs:16

//! Escritor mínimo de PDF a partir de JPEGs (uma imagem por página, filtro
//! DCTDecode, sem recomprimir). É o `img2pdf` dos scripts de SlideShare
//! (estudo 50) sem dependência nova.

use anyhow::anyhow;

pub struct JpegInfo {
    pub width: u32,
    pub height: u32,
    pub components: u8,
}

/// Lê largura/altura/canais do marcador SOF do JPEG.
pub fn jpeg_info(data: &[u8]) -> anyhow::Result<JpegInfo> {
    if data.len() < 4 || data[0] != 0xFF || data[1] != 0xD8 {
        return Err(anyhow!("nao e um JPEG"));
    }
    let mut i = 2usize;
    while i + 9 < data.len() {
        if data[i] != 0xFF {
            i += 1;
            continue;
        }
        let marker = data[i + 1];
        if marker == 0xFF {
            i += 1;
            continue;
        }
        // marcadores sem payload
        if (0xD0..=0xD9).contains(&marker) || marker == 0x01 {
            i += 2;
            continue;
        }
        let len = u16::from_be_bytes([data[i + 2], data[i + 3]]) as usize;

View on GitHub (pinned to 8600b91f42)