Arithmetic operations choose arch-dependant type and overflow in 32bit

オープン
#804 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #946 @eyupcanakman による — マージされずにクローズ

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
go
領域
backend

調査の方向性

まず、i386/golang コンテナ内で 32bit_test.go の再現テストを expr に対して実行し、val64 + 0 式がどのようにオペランドの型を選択するかを追跡します。32 ビットアーキテクチャで、他の整数演算を壊すことなく、テストが -2147483648 ではなく 2147483648 を報告すれば完了です。

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

説明

bug

Hi, thank you for your work and for reading. I found that arithmetic operations choose architecture-dependant types in some circumstances and can cause overflows in 32bit architectures.

Example

Given a value a of type int64 and a value b of any integer type, if we run the expression a+b then we will get int(a)+int(b), which can overflow in 32bit architectures.

Reproduction steps

First, save the following file as 32bit_test.go:

package main

import (
        "fmt"
        "math"
        "testing"

        "github.com/expr-lang/expr"
)

func TestOverflows32bit(t *testing.T) {
        p, err := expr.Compile("val64 + 0")
        if err != nil {
                t.Fatalf("compile error: %v", err)
        }
        val, err := expr.Run(p, map[string]any{
                "val64": int64(math.MaxInt32 + 1),
        })
        if err != nil {
                t.Fatalf("run error: %v", err)
        }
        str := fmt.Sprint(val)
        if str != "2147483648" {
                t.Fatalf("expected %v, got %v", "2147483648", val)
        }
}

Second, run it in a 32bit architecture docker container:

docker run --name go32 --rm -dit --platform linux/386 i386/golang:latest bash
docker exec go32 mkdir -p /go/src/test
docker cp 32bit_test.go go32:/go/src/test/main_test.go
docker exec -it go32 bash
> cd /go/src/test/
> go mod init
> go get github.com/expr-lang/expr
> go test
--- FAIL: TestOverflows32bit (0.01s)
    main_test.go:24: expected 2147483648, got -2147483648
FAIL
exit status 1
FAIL    test    0.043s
主要言語
Go
スター
8.1k
フォーク
544
平均マージ
1日 9時間
マージ済み PR(30日)
25

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

expr-lang/expr のほかの issue

expr-lang/expr の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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