A few cases of left shifting negative integers - Mailing list pgsql-hackers

From Piotr Stefaniak
Subject A few cases of left shifting negative integers
Date
Msg-id BLU437-SMTP571E09582ECE3097567E46F2650@phx.gbl
Whole thread Raw
Responses Re: A few cases of left shifting negative integers  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hello,

during my testing I've found cases of left-shifting negative integers
during run-time and I was recently encouraged to post a report of them,
so here it is (done against 960ea971e66bcd621ba88841b4cb85c7f0e7c383).

Back-traces are included in the attached file. If any kind of
information seems wrong or missing, just let me know and I'll try to fix
it. (In hindsight, I should have also printed the objects' values).

src/backend/utils/adt/int8.c:1262
Datum
int8shl(PG_FUNCTION_ARGS)
{
    int64        arg1 = PG_GETARG_INT64(0);
    int32        arg2 = PG_GETARG_INT32(1);

    PG_RETURN_INT64(arg1 << arg2);
}

src/backend/utils/adt/int.c:1258
Datum
int4shl(PG_FUNCTION_ARGS)
{
    int32        arg1 = PG_GETARG_INT32(0);
    int32        arg2 = PG_GETARG_INT32(1);

    PG_RETURN_INT32(arg1 << arg2);
}

src/backend/utils/adt/int.c:1320
Datum
int2shl(PG_FUNCTION_ARGS)
{
    int16        arg1 = PG_GETARG_INT16(0);
    int32        arg2 = PG_GETARG_INT32(1);

    PG_RETURN_INT16(arg1 << arg2);
}

src/backend/utils/adt/network.c:1523
        /*
         * If input is narrower than int64, overflow is not possible, but we
         * have to do proper sign extension.
         */
        if (carry == 0 && byte < sizeof(int64))
            res |= ((int64) -1) << (byte * 8);

src/backend/utils/cache/inval.c:587
    else if (msg->id == SHAREDINVALSMGR_ID)
    {
        /*
         * We could have smgr entries for relations of other databases, so no
         * short-circuit test is possible here.
         */
        RelFileNodeBackend rnode;

        rnode.node = msg->sm.rnode;
        rnode.backend = (msg->sm.backend_hi << 16) | (int) msg->sm.backend_lo;
        smgrclosenode(rnode);
    }

src/timezone/localtime.c:127
static long
detzcode(const char *codep)
{
    long        result;
    int            i;

    result = (codep[0] & 0x80) ? ~0L : 0;
    for (i = 0; i < 4; ++i)
        result = (result << 8) | (codep[i] & 0xff);
    return result;
}

src/common/pg_lzcompress.c:404
src/common/pg_lzcompress.c:637
src/common/pg_lzcompress.c:651
These I can't conveniently reproduce and present to you, because a
couple of macros are involved.

Attachment

pgsql-hackers by date:

Previous
From: David Fetter
Date:
Subject: Re: [PATCH] postgres_fdw extension support
Next
From: Tom Lane
Date:
Subject: Re: Make HeapTupleSatisfiesMVCC more concurrent