0

I'm trying to compare numerical values of two pointers with ">=" operator but instead of comparing them it adds some random values to the first.

void obj::Loader::calculate_size(const char* start_ptr, const char* end_ptr, atomic_long& v, atomic_long& vt, atomic_long& vn, atomic_long& f) {
    while (start_ptr >= end_ptr) {
        if (*start_ptr == 'v') {
            start_ptr++;
            if (*start_ptr == ' ') {
                v++;
                start_ptr += 27;
            }
            else if (*start_ptr == 't') {
                vt++;
                start_ptr += 18;
            }
            else if (*start_ptr == 'n') {
                vn++;
                start_ptr += 21;
            }
        }
        else if (*start_ptr == 'f') {
            start_ptr++;
            if (*start_ptr == ' ') {
                f += 3;
                start_ptr += 17;
            }
        }
        start_ptr++;
    }
}

This function is supposed to count all instances of these letters in a part of a memory-mapped file (from "start_ptr" to "end_ptr"). Knowing the minimal length at certain portions of the file I decided to increment the "start_ptr" by more than one sometimes but by doing that I can't rely on "!=" operator to stop the loop.

The problem is ">=" doesn't really work as it would on integers and it just crashes the whole thing. Are there any alternatives?

Big Temp
  • 434
  • 4
  • 12
  • 2
    Did you mean to write `<=` instead of `>=`? The loop never stops otherwise (pointer arithmetic issues aside). – walnut Sep 20 '19 at 02:38
  • Regarding pointer arithmetic: Although it is unlikely that this will cause you problems, increasing the pointer the way you are doing [may technically be undefined behavior](https://stackoverflow.com/questions/10473573/why-is-out-of-bounds-pointer-arithmetic-undefined-behaviour). – walnut Sep 20 '19 at 02:49
  • @uneven_mark You mean the loop never starts – M.M Sep 20 '19 at 03:08
  • @M.M Either or. – walnut Sep 20 '19 at 03:13

2 Answers2

2

Given that the loop condition is start_ptr >= end_ptr, and considering that the loop body only ever increments start_ptr, if the loop is ever entered, then start_ptr can never become smaller than end_ptr and therefore the loop is infinite. The behaviour of the program is undefined.

It would be rather unconventional for "start" to be after the end, so I suspect that this is a mistake in the logic.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Oh. Right. I typed it in reverse. I should probably get some sleep, thank you. Please someone delete this o.o – Big Temp Sep 20 '19 at 02:42
  • The question can be closed as typo, which is nearly as good as deleting – M.M Sep 20 '19 at 03:10
1

The comparison looks to be wrong to me: instead of

while (start_ptr >= end_ptr) {
    // [...]
    start_ptr++;
}

I would expect the natural condition to be:

while (start_ptr < end_ptr) {
    // [...]
    start_ptr++;
}

I suspect the crash in your program is not due to the pointer comparison directly, but is a side effect of something unexpected that happens when the loop is not entered.

NicholasM
  • 4,557
  • 1
  • 20
  • 47