[Poppler-bugs] [Bug 37745] Problems in file goo/GooString.cc

bugzilla-daemon at freedesktop.org bugzilla-daemon at freedesktop.org
Tue May 31 00:21:55 PDT 2011


https://bugs.freedesktop.org/show_bug.cgi?id=37745

--- Comment #2 from fusionefredda at yahoo.it 2011-05-31 00:21:55 PDT ---
(In reply to comment #1)
> Can you please explain why
>   x = (unsigned char)*p1 - (unsigned char)*p2;
> is correct if neither p1 nor p2 are unsigned chars?

I will provide a code example:

#include <stdio.h>
#include <string.h>

int cmp(const char*s, const char *sA) {
    int n1, i, x;
    const char *p1, *p2;

    n1 = strlen(s);
    for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) {
        x = *p1 - *p2;
        if (x != 0) {
            return x;
        }
    }
    if (i < n1) {
        return 1;
    }
    if (*p2) {
        return -1;
    }
    return 0;
}

int unsigned_cmp(const char*s, const char *sA) {
    int n1, i, x;
    const char *p1, *p2;

    n1 = strlen(s);
    for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) {
        x = (unsigned char)*p1 - (unsigned char)*p2;
        if (x != 0) {
            return x;
        }
    }
    if (i < n1) {
        return 1;
    }
    if (*p2) {
        return -1;
    }
    return 0;
}

int main()
{
    const char* str1 = "17_scuole";
    const char* str2 = "þÿ";

    int ret = strcmp(str1, str2);
    printf("strcmp ret: %d\n", ret);

    ret = cmp(str1, str2);
    printf("cmp ret: %d\n", ret);

    ret = unsigned_cmp(str1, str2);
    printf("unsigned_cmp ret: %d\n", ret);
}





The output is:

strcmp ret: -1
cmp ret: 110
unsigned_cmp ret: -146

A correct returned value is either -1 or -146, a positive value is instead
incorrect. This fools the bsearch (binary tree sarch), pointing the search for
the entry "17_scuole" to a incorrect branch, resulting in a NULL even if the
entry "17_scuole" is present. I hope this example explains clearly the problem. 
Regards.

-- 
Configure bugmail: https://bugs.freedesktop.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the Poppler-bugs mailing list