oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.35k stars 2.78k forks source link

Segmentation fault at address 0x00000005 #15251

Closed AwesomeKalin closed 2 days ago

AwesomeKalin commented 2 days ago

How can we reproduce the crash?

import { number, confirm, input } from '@inquirer/prompts';
import type { BunFile } from 'bun';

let randNum: number = Math.round(Math.random() * 10);
let attempts: number = 3;

let score: number = 0;

type Score = {
    name: string;
    score: number;
}

const highScoreLoad: BunFile = Bun.file('./highscore.json');
let highScoreData: {
    "1": Score;
    "2": Score;
    "3": Score;
    "4": Score;
    "5": Score;
} = {
    "1": {
        name: '',
        score: 0,
    },
    "2": {
        name: '',
        score: 0,
    },
    "3": {
        name: '',
        score: 0,
    },
    "4": {
        name: '',
        score: 0,
    },
    "5": {
        name: '',
        score: 0,
    },
};

const printLeaderboard = () => console.log(`Leaderboard:\n1st: ${highScoreData[1].name}, ${highScoreData[1].score} points\n2nd: ${highScoreData[2].name}, ${highScoreData[2].score} points\n3rd: ${highScoreData[3].name}, ${highScoreData[3].score} points\n4th: ${highScoreData[4].name}, ${highScoreData[4].score} points\n5th: ${highScoreData[5].name}, ${highScoreData[5].score} points\n`);

if (await highScoreLoad.exists()) {
    highScoreData = await highScoreLoad.json();
    printLeaderboard();
}

while (true) {
    const guess: number | undefined = await number({
        message: 'Guess a number between 0-10',
        required: true,
    });

    if (guess === undefined) {
        console.log('Please enter a number!');
        continue;
    }

    if (guess < randNum) {
        console.log('Too Low!');
        attempts--;
    }

    if (guess > randNum) {
        console.log('Too High!');
        attempts--;
    }

    if (guess === randNum) {
        console.log('Correct!');
        score++;
        await playAgain(true);
    }

    if (attempts === 0) {
        console.log(`Incorrect! The number was ${randNum}`);
        await saveScore(score);
        score = 0;
        await playAgain(false);
    }
}

async function playAgain(ifSuccess: boolean) {
    printLeaderboard();
    if (await confirm({ message: 'Do you want to play again?' })) {
        attempts = 3;
        randNum = Math.round(Math.random() * 10);
    } else {
        if (ifSuccess) {
            await saveScore(score);
        }
        process.exit(0);
    }
}

async function saveScore(score: number) {
    let name: string;

    if (score >= highScoreData[5].score) {
        name = await input({
            message: 'You\'re on the leaderboard! Please enter your name to be added to it!',
            required: true,
        });
    } else {
        return;
    }

    if (score >= highScoreData[1].score) {
        highScoreData[5] = highScoreData[4];
        highScoreData[4] = highScoreData[3];
        highScoreData[3] = highScoreData[2];
        highScoreData[2] = highScoreData[1];
        highScoreData[1] = {
            name,
            score
        };
    } else if (score >= highScoreData[2].score) {
        highScoreData[5] = highScoreData[4];
        highScoreData[4] = highScoreData[3];
        highScoreData[3] = highScoreData[2];
        highScoreData[2] = {
            name,
            score
        };
    } else if (score >= highScoreData[3].score) {
        highScoreData[5] = highScoreData[4];
        highScoreData[4] = highScoreData[3];
        highScoreData[3] = {
            name,
            score
        };
    } else if (score >= highScoreData[4].score) {
        highScoreData[5] = highScoreData[4];
        highScoreData[4] = {
            name,
            score
        };
    } else if (score >= highScoreData[5].score) {
        highScoreData[5] = {
            name,
            score
        };
    }

    await Bun.write('./highscore.json', JSON.stringify(highScoreData));
    return;
}

Bun always crashes after a certain amount of guesses

Relevant log output

Stack Trace (bun.report)

Bun v1.1.34 (5e5e7c6) on windows x86_64 [AutoCommand]

Segmentation fault at address 0x00000005

Features: Bun.stderr, Bun.stdin, Bun.stdout, fetch, jsc, spawn, transpiler_cache, tsconfig, tsconfig_paths, tsconfig_paths

Sentry Issue: BUN-85

nektro commented 2 days ago

duplicate of https://github.com/oven-sh/bun/issues/10844