2
void page_kernel_only(int16_t page){

if(mode != KERNEL)
  {
    return;
  }
page = page << 5;
page = page >> 5;
int16_t help = 8192;
help = help & page_table[page];

if(help == 0)
{
    page_table[page] += 8192;
}

}

i get an "array subscript is below array bounds" warning and i dont know why, because a previous part of the code dont give me such a warning

void open_page(int16_t page){
if(mode != KERNEL)
{
return;
}
page = page << 5;
page = page >> 5;
int16_t help = 16384;
help = help & page_table[page];
if(help == 1)
{
    return;
}
page_table[help] += 16384;

}

5
  • mmu.c:154:26: warning: array subscript is below array bounds [-Warray-bounds] help = help & page_table[page]; Commented Jul 13, 2015 at 19:53
  • Is that warning at compile-time or runtime? Commented Jul 13, 2015 at 19:54
  • Since the variable page is an "int16_t" it's signed. If it's negative the right shift may shift '1' bits in at the most significant bit. I'm not sure what your shifting is actually supposed to accomplish. Commented Jul 13, 2015 at 19:57
  • Right shifting a signed integer is implementation defined. Left shifting a signed integer will also generate implementation defined behaviour if the sign is "shifted out". Commented Jul 13, 2015 at 20:07
  • page = page << 5; page = page >> 5; What is the purpose of these two statements? If page << 5 overflows, the behavior is undefined. If it doesn't, the net result is that page is unchanged. If you're trying to clear the 5 low-order bits, you'd want to do the right shift followed by the left shift -- but a bitwise & would be a clearer way to do that. Commented Jul 13, 2015 at 20:52

1 Answer 1

1

You have page defined as a 16-bit signed int. So if the value is greater than 1024, when you shift left by 5 then shift right by 5 you risk of the value becoming negative, which would generate the warning.

Edit:

If you want to ensure that the top 5 bits of page are 0, you need to do this:

page = page & 0x03FF;

Edit2:

page should be defined as uint16_t instead of int16_t. That should take care of the warnings.

Sign up to request clarification or add additional context in comments.

4 Comments

i need it to compile without errors and warnings, is there a workaround to shift back and forth without getting a warning?
Right shifting a negative value is implementation defined. There is no guarantee for not preserving the sign, nor shifting zeros in.
int16_t is the guideline for this task, we "shall" learn how to handle those problems and now stackoverflow tought me how to handle them
@noaH Fyi, per the C11 §6.5.7, p4: "The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2^E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2^E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.". In short, if page is non-negative, and page << 5 is not representable as an int16_t with value page * 2^5, you've already invoked UB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.