Issue Details (XML | Word | Printable)

Key: PCC-355
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Anders Magnusson
Reporter: Iain Hibbert
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
pcc

pointer arithmetic gives wrong result

Created: 11/Jun/11 06:35 PM   Updated: 07/Jul/11 09:53 AM
Component/s: None
Affects Version/s: None
Fix Version/s: None

Environment: NetBSD/i386


 Description  « Hide
An equivalent construct to the following code was being used to rebase a list of pointers,
where the actual data was in a separate buffer which had been realloc()ed.

#include <stdio.h>

struct hdr {
    int length;
    int offset;
};

struct hdr *new = (struct hdr *)0xbb300000;
struct hdr *old = (struct hdr *)0xbb700000;

int
main(int ac, char *av[])
{
    struct hdr *p, *q;

    /*
     * this value is naturally aligned
     */
    p = (struct hdr *)0xbb7fff78,
    q = p - old + new;
    printf("1. %p -> %p\n", p, q);

    /*
     * this value is not
     */
    p = (struct hdr *)0xbb7fff34;
    q = p - old + new;
    printf("2. %p -> %p\n", p, q);

    return 0;
}

so, as the pointers are all known to be greater than the old buffer start, the
new value is calculated by subtracting the old buffer start, then adding in the
new one.

When compiled with pcc and run, this program prints the following

1. 0xbb7fff78 -> 0xbb3fff78
2. 0xbb7fff34 -> 0xbb3fff30

but the second value is wrong.. it should be "0xbb3fff34" which seems to be
caused by the value not being naturally aligned to an exact number of structures..

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Anders Magnusson made changes - 01/Jul/11 05:26 PM
Field Original Value New Value
Assignee Anders Magnusson [ ragge ]
Anders Magnusson made changes - 01/Jul/11 05:32 PM
Issue Type Bug [ 1 ] New Feature [ 2 ]
Priority Major [ 3 ] Minor [ 4 ]
Anders Magnusson made changes - 07/Jul/11 09:53 AM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]