Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Signed overflow in cbor_value_get_int64() / cbor_value_get_int() for out-of-range negative integers

オープン 初心者向け
#347 コメント 2 件 リアクション 0 件 担当者 1 名 GitHub で見る

@thiagomacieira がすでに取り組んでいます。

2026年10月26日 から。

  • #349 @thiagomacieira による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

影響を受ける関数は cbor_value_get_int64() と cbor_value_get_int() で、src/cbor.h にあります。まず、負の整数の処理と未チェック変換に関するドキュメントを読んでください。報告された境界ケースを UndefinedBehaviorSanitizer で再現し、その後、両方の入力を対象とするテストを追加してください。両方の関数が符号付き整数オーバーフローを回避し、sanitizer を使ってテストが通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

cbor_value_get_int64() and cbor_value_get_int() in src/cbor.h do

*result = -*result - 1;

for negative integers. If the encoded magnitude is 2^63 (or 2^31 for the int version), *result is already INT64_MIN / INT_MIN at that point, so the negation overflows. That's UB, and it's reachable from untrusted input through the public API. The only precondition is cbor_value_is_integer(), which these inputs satisfy.

Inputs:

  • 3b 80 00 00 00 00 00 00 00, i.e. -2^63 - 1, hits cbor_value_get_int64()
  • 3a 80 00 00 00, i.e. -2^31 - 1, hits cbor_value_get_int()

With clang -fsanitize=undefined on current main (da6421f):

src/cbor.h:437:19: runtime error: negation of -9223372036854775808 cannot be represented in type 'int64_t' (aka 'long')
src/cbor.h:446:19: runtime error: negation of -2147483648 cannot be represented in type 'int'

Minimal repro:

#include <stdio.h>
#include <stdint.h>
#include "cbor.h"

int main(void)
{
    static const uint8_t data[] = { 0x3b, 0x80, 0, 0, 0, 0, 0, 0, 0 };
    CborParser parser;
    CborValue it;
    int64_t v;
    if (cbor_parser_init(data, sizeof(data), 0, &parser, &it) || !cbor_value_is_integer(&it))
        return 1;
    cbor_value_get_int64(&it, &v);
    printf("%lld\n", (long long)v);
    return 0;
}

I know the docs say to use the _checked variants if the value might not fit, and those are fine. But the unchecked ones are documented as returning a truncated value in that case, not as UB, and in practice a sanitizer build aborts. An optimising compiler is also free to assume the overflow can't happen.

I ran into this while writing CBMC harnesses and fuzzing tinycbor. Happy to open a PR with a fix (~*result instead of -*result - 1, same value, no overflow) and a test if that sounds good.

主要言語
C++
スター
641
フォーク
225
平均マージ
2日 3時間
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

intel/tinycbor のほかの issue

intel/tinycbor の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。