The docs for "PRAGMA application_id" read:

> The application_id PRAGMA is used to query or set the 32-bit unsigned
> big-endian "Application ID" integer located at [...].

However, it appears that the argument to this pragma is interpreted as a
_signed_ integer, not an unsigned integer. In particular, values outside
the ranged of a 32-bit signed integer are treated as zero, and negative
values within the range of a 32-bit signed integer are accepted but
interpreted as unsigned by file(1).

Meanwhile, magic.txt says that application_id _is_ a signed integer, and
pragma.c seems to imply it (it says that schema_version and user_version
are signed, and application_id goes through the same code path as far as
I can tell).

Is this an error in the documentation?

The following script reproduces the behavior:

    #!/bin/sh
    set -eu

    printf '$ %s\n' 'sqlite3 --version'
    sqlite3 --version

    tmpdir="$(mktemp -d)"
    tmpfile="${tmpdir}/test.db"

    run() {
        printf '\n'
        rm -f "${tmpfile}"
        printf 'application_id: %s\n' "$1"
        printf 'SQLite says: '
        printf '
            PRAGMA application_id = 777;
            PRAGMA application_id = %s;
            PRAGMA application_id;
        ' "$1" | sqlite3 "${tmpfile}"
        printf 'file(1) says: '
        file -b "${tmpfile}"
        printf 'raw bytes are: '
        xxd -i -s 68 -l 4 <"${tmpfile}"
    }

    # Values between 0 and 2^31 - 1, inclusive, work fine.
    run 0
    run 3
    run 2147483647

    # Values larger than 2^31 - 1 are treated as zero.
    run 2147483648
    run 2147483649
    run 4294967295
    run 4294967296
    run 4294967297

    # Values between -2^31 and -1, inclusive, are treated inconsistently
    # between SQLite and file(1).
    run -2147483648
    run -2147483647
    run -3

    # Values smaller than -2^31 are treated as zero.
    run -2147483649
    run -4294967295
    run -4294967296

    rm "${tmpfile}"
    rmdir "${tmpdir}"

(End of script.)

A copy of this script and its output on my machine are available at:

    https://gist.github.com/wchargin/ac2a339f002a9604018bbc3b3be59ffb

I have the following version of sqlite3...

    3.11.0 2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f

...running on GNU/Linux (Linux Mint 18.2).

I have searched the issue tracker and found no tickets matching
"application_id". I see one mailing list thread from 2014-02-06 that
mentions this in the context of another question, with no replies.

Best,
WC
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to