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

Add: test to ensure correct swapping of the magic number #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

laustam
Copy link
Contributor

@laustam laustam commented Jun 20, 2024

I am suggesting this test addition as a result of a mistake made by one of my students.

Their implementation went as follows:

uint8_t magic_num_buf[4];
fread(magic_num_buf, sizeof(byte_t), 4, fp);
uint32_t magic_num = read_uint32(magic_num_buf);

if (!(magic_num == MAGIC_NUMBER || swap_uint32(magic_num) == MAGIC_NUMBER))
  // not an ijvm file
  return NULL;
}

The problematic part here is that the student does not fully understand the notion of endianness; they check whether the unswapped or swapped version of the first four bytes equals MAGIC_NUMBER (0x1deadfad) to determine whether the file is a valid IJVM binary. This is incorrect: files starting with 0xaddfea1d are not valid IJVM binaries.

To address this misconception, I propose a test, magicnum_swap, which asserts that initializing an IJVM with a given file that starts with 0xaddfea1d returns NULL (ie. the initialization fails).

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.

1 participant