Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of bounds when using the ast package in a file with no empty last line #3537

Open
thetarnav opened this issue May 5, 2024 · 0 comments
Labels

Comments

@thetarnav
Copy link
Contributor

I think that odin/parser when parsing declarations assumes that after each declaration there will be a \n, because the end position for declarations is extended by one.
When there is no newline after the last declaration, and I try to print it, using the offsets, I get an out of bounds error.

code

package main

import "core:fmt"
import "core:odin/ast"
import "core:odin/parser"

main :: proc() {
    pkg, ok := parser.collect_package(".")
    if !ok do panic("Failed to collect package\n")

    ok = parser.parse_package(pkg)
    if !ok do panic("Failed to parse package\n")

    for file_name, file in pkg.files {
        for decl in file.decls {
            fmt.printf("%s\n", file.src[decl.pos.offset:decl.end.offset]) // <- out of bounds error
        }
    }
} // <- no newline afterwards

error

$ odin run .
import "core:fmt"
import "core:odin/ast"
import "core:odin/parser"
/path/main.odin(16:40) Invalid slice indices 82:443 is out of range 0..<442
Illegal instruction (core dumped)

report

	Odin:    dev-2024-05:1e5267c8e
	OS:      Ubuntu 22.04.4 LTS, Linux 6.5.0-28-generic
	CPU:     Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
	RAM:     7631 MiB
	Backend: LLVM 17.0.6

I know that this is an issue with my code, but the praser could probably prevent that?
It's weird to have nodes that extend the file bounds.

@github-actions github-actions bot added the stale label Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant