Giter VIP home page Giter VIP logo

Comments (2)

charlesnicholson avatar charlesnicholson commented on May 29, 2024 1

Thanks for filing it. FWIW, I think the code is safe, but I understand why UBsan is yelling at me. I'm unconditionally decrementing dec_len in the while loop condition, but if dec_len starts at zero, then it underflows past zero and that's surprising / sometimes-bad.

In this case nothing outside of the while loop reads dec_len, and the while loop will be skipped if dec_len is 0, so it's leaving an underflowed value in its wake but one that's never read by any code.

  while (dec_len--) {
    cobs_byte_t const byte = src[src_idx];
    if (byte) {
      dst[dst_idx] = byte;
      if (++dst_idx >= enc_max) { return COBS_RET_ERR_EXHAUSTED; }
      ++code;
    }

    if ((byte == 0) || (code == 0xFF)) {
      dst[dst_code_idx] = (cobs_byte_t)code;
      dst_code_idx = dst_idx;
      code = 1;

      if ((byte == 0) || dec_len) {
        if (++dst_idx >= enc_max) { return COBS_RET_ERR_EXHAUSTED; }
      } else {
        need_advance = !dec_len;
      }
    }
    ++src_idx;
  }

  ctx->cur = dst_idx;
  ctx->code = code;
  ctx->code_idx = dst_code_idx;
  ctx->need_advance = need_advance;
  return COBS_RET_SUCCESS;
}

I'll guard against 0 with a success-early-out at the top of the function- even though it's safe right now, it's just begging to give someone a Bad Surprise if they're hacking around in the code, or if I rework it in the future.

from nanocobs.

charlesnicholson avatar charlesnicholson commented on May 29, 2024

v0.0.4 release here https://github.com/charlesnicholson/nanocobs/releases/tag/v0.0.4 with this fix and your other overrun fix.

from nanocobs.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.