Skip to content

Conversation

@emilycares
Copy link
Contributor

Remove parsing of attribute_name_index and attribute_length from sourcefile_attribute_parser.
These fields don't need to be parsed here.

Remove parsing of attribute_name_index and attribute_length from
sourcefile_attribute_parser.
These fields don't need to be parsed here.
@Palmr
Copy link
Owner

Palmr commented May 15, 2025

Doesn't this differ from the spec?

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.10

Which says the SourceFile attribute struct is

SourceFile_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 sourcefile_index;
}

@emilycares
Copy link
Contributor Author

emilycares commented May 15, 2025

It should not. This implementation works with a Java 8 compiler and also a Java 21 compiler.
I have recompiled the class with both compilers. And ran the tests.

Also, it seems to be that all other attribute parsers don't parse the first to fields to. For example:

pub fn code_attribute_parser(input: &[u8]) -> Result<(&[u8], CodeAttribute), Err<&[u8]>> {
let (input, max_stack) = be_u16(input)?;
let (input, max_locals) = be_u16(input)?;
let (input, code_length) = be_u32(input)?;
let (input, code) = take(code_length)(input)?;

Yes, it is weird that it is in the specification but does not need to be parsed here. At this point, I don't know why I don't have to parse these... And if I try, to parse then it errors.

u2 attribute_name_index;
u4 attribute_length;

@Palmr
Copy link
Owner

Palmr commented May 28, 2025

Name and length get parsed generically for all attributes here:

pub fn attribute_parser(input: &[u8]) -> Result<(&[u8], AttributeInfo), Err<&[u8]>> {
let (input, attribute_name_index) = be_u16(input)?;
let (input, attribute_length) = be_u32(input)?;
let (input, info) = take(attribute_length)(input)?;

So the individual attribute parsers don't need to parse it themselves.

So your change is correct, good spot 👍

@Palmr Palmr merged commit 3639d2d into Palmr:master May 28, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants