Commit 989ed582 authored by gwenn's avatar gwenn

Fix some potential int overflows on 64-bit arch.

parent 8967236c
......@@ -45,7 +45,7 @@ type Backup struct {
// Step copies up to N pages between the source and destination databases.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep)
func (b *Backup) Step(npage int) error {
func (b *Backup) Step(npage int32) error {
if b == nil {
return errors.New("nil sqlite backup")
}
......@@ -78,7 +78,7 @@ func (b *Backup) Status() BackupStatus {
// Sleeping is disabled if 'sleepNs' is zero or negative.
// Notification is disabled if 'c' is null.
// (See http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep, sqlite3_backup_remaining and sqlite3_backup_pagecount)
func (b *Backup) Run(npage int, sleepNs time.Duration, c chan<- BackupStatus) error {
func (b *Backup) Run(npage int32, sleepNs time.Duration, c chan<- BackupStatus) error {
var err error
for {
err = b.Step(npage)
......
......@@ -54,6 +54,7 @@ import "C"
import (
"fmt"
"math"
"reflect"
"unsafe"
)
......@@ -138,6 +139,10 @@ func (c *FunctionContext) ResultBool(b bool) {
// ResultBlob sets the result of an SQL function.
// (See sqlite3_result_blob, http://sqlite.org/c3ref/result_blob.html)
func (c *Context) ResultBlob(b []byte) {
if i64 && len(b) > math.MaxInt32 {
C.sqlite3_result_error_toobig((*C.sqlite3_context)(c))
return
}
var p *byte
if len(b) > 0 {
p = &b[0]
......@@ -189,7 +194,11 @@ func (c *FunctionContext) ResultErrorCode(e Errno) {
// ResultInt sets the result of an SQL function.
// (See sqlite3_result_int, http://sqlite.org/c3ref/result_blob.html)
func (c *Context) ResultInt(i int) {
if i64 && (i > math.MaxInt32 || i < math.MinInt32) {
C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i))
} else {
C.sqlite3_result_int((*C.sqlite3_context)(c), C.int(i))
}
}
// ResultInt sets the result of an SQL function.
......@@ -463,7 +472,7 @@ const sqliteDeterministic = 0x800 // C.SQLITE_DETERMINISTIC
// CreateScalarFunction creates or redefines SQL scalar functions.
// TODO Make possible to specify the preferred encoding
// (See http://sqlite.org/c3ref/create_function.html)
func (c *Conn) CreateScalarFunction(functionName string, nArg int, deterministic bool, pApp interface{},
func (c *Conn) CreateScalarFunction(functionName string, nArg int32, deterministic bool, pApp interface{},
f ScalarFunction, d DestroyDataFunction) error {
var eTextRep C.int = C.SQLITE_UTF8
if deterministic {
......@@ -491,7 +500,7 @@ func (c *Conn) CreateScalarFunction(functionName string, nArg int, deterministic
// CreateAggregateFunction creates or redefines SQL aggregate functions.
// TODO Make possible to specify the preferred encoding
// (See http://sqlite.org/c3ref/create_function.html)
func (c *Conn) CreateAggregateFunction(functionName string, nArg int, pApp interface{},
func (c *Conn) CreateAggregateFunction(functionName string, nArg int32, pApp interface{},
step StepFunction, final FinalFunction, d DestroyDataFunction) error {
fname := C.CString(functionName)
defer C.free(unsafe.Pointer(fname))
......
......@@ -534,7 +534,7 @@ func (c *Conn) Close() error {
rv := C.sqlite3_close(c.db)
if rv != C.SQLITE_OK {
Log(int(rv), "error while closing Conn")
Log(int32(rv), "error while closing Conn")
return c.error(rv, "Conn.Close")
}
c.db = nil
......
......@@ -347,6 +347,9 @@ func (s *Stmt) BindByIndex(index int, value interface{}) error {
if NullIfEmptyString && len(value) == 0 {
rv = C.sqlite3_bind_null(s.stmt, i)
} else {
if i64 && len(value) > math.MaxInt32 {
return s.specificError("string too big: %d at index %d", len(value), index)
}
cs, l := cstring(value)
rv = C.my_bind_text(s.stmt, i, cs, l)
}
......@@ -369,6 +372,9 @@ func (s *Stmt) BindByIndex(index int, value interface{}) error {
case float64:
rv = C.sqlite3_bind_double(s.stmt, i, C.double(value))
case []byte:
if i64 && len(value) > math.MaxInt32 {
return s.specificError("blob too big: %d at index %d", len(value), index)
}
var p *byte
if len(value) > 0 {
p = &value[0]
......@@ -1139,7 +1145,7 @@ func (s *Stmt) finalize() error {
}
rv := C.sqlite3_finalize(s.stmt)
if rv != C.SQLITE_OK {
Log(int(rv), "error while finalizing Stmt")
Log(int32(rv), "error while finalizing Stmt")
return s.error(rv, "Stmt.finalize")
}
s.stmt = nil
......
......@@ -285,7 +285,7 @@ func goXProgress(udp unsafe.Pointer) C.int {
// ProgressHandler registers or clears a query progress callback.
// The progress callback will be invoked every numOps opcodes.
// (See http://sqlite.org/c3ref/progress_handler.html)
func (c *Conn) ProgressHandler(f ProgressHandler, numOps int, udp interface{}) {
func (c *Conn) ProgressHandler(f ProgressHandler, numOps int32, udp interface{}) {
if f == nil {
c.progressHandler = nil
C.sqlite3_progress_handler(c.db, 0, nil, nil)
......@@ -347,7 +347,7 @@ func Complete(sql string) bool {
// Log writes a message into the error log established by ConfigLog method.
// (See http://sqlite.org/c3ref/log.html)
func Log(err /*Errno*/ int, msg string) {
func Log(err /*Errno*/ int32, msg string) {
cs := C.CString(msg)
defer C.free(unsafe.Pointer(cs))
C.my_log(C.int(err), cs)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment