chiark
/
gitweb
/
~ian
/
chiark-tcl.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
use of int and overflow review
[chiark-tcl.git]
/
cdb
/
writeable.c
diff --git
a/cdb/writeable.c
b/cdb/writeable.c
index 9cdd0095bd615fd1a1359e2ee145a81e12487513..0df1b2799ae55a59e60672c23e99fe56f97304aa 100644
(file)
--- a/
cdb/writeable.c
+++ b/
cdb/writeable.c
@@
-20,6
+20,8
@@
#include "chiark_tcl_cdb.h"
#include "chiark_tcl_cdb.h"
+#define KEYLEN_MAX (INT_MAX/2)
+
#define ftello ftell
#define fseeko fseek
#define ftello ftell
#define fseeko fseek
@@
-44,7
+46,8
@@
typedef struct Pathbuf {
#define MAX_SUFFIX 5
static void pathbuf_init(Pathbuf *pb, const char *pathb) {
#define MAX_SUFFIX 5
static void pathbuf_init(Pathbuf *pb, const char *pathb) {
- int l= strlen(pathb);
+ size_t l= strlen(pathb);
+ assert(l < INT_MAX);
pb->buf= TALLOC(l + MAX_SUFFIX + 1);
memcpy(pb->buf, pathb, l);
pb->sfx= pb->buf + l;
pb->buf= TALLOC(l + MAX_SUFFIX + 1);
memcpy(pb->buf, pathb, l);
pb->sfx= pb->buf + l;
@@
-250,7
+253,7
@@
static int readlognum(FILE *f, int delim, int *num_r) {
*p= 0;
errno=0; ul= strtoul(numbuf, &ep, 10);
*p= 0;
errno=0; ul= strtoul(numbuf, &ep, 10);
- if (*ep || errno || ul >=
INT_MAX/2
) return -2;
+ if (*ep || errno || ul >=
KEYLEN_MAX
) return -2;
*num_r= ul;
return 0;
}
*num_r= ul;
return 0;
}
@@
-313,7
+316,7
@@
static int readstorelogrecord(FILE *f, HashTable *ht,
static int writerecord(FILE *f, const char *key, const HashValue *val) {
int r;
static int writerecord(FILE *f, const char *key, const HashValue *val) {
int r;
- r= fprintf(f, "+%d,%d:%s->", strlen(key), val->len, key);
+ r= fprintf(f, "+%d,%d:%s->",
(int)
strlen(key), val->len, key);
if (r<0) return -1;
r= fwrite(val->data, 1, val->len, f);
if (r<0) return -1;
r= fwrite(val->data, 1, val->len, f);
@@
-859,6
+862,9
@@
static int update(Tcl_Interp *ip, Rw *rw, const char *key,
HashValue *val;
int rc, r;
HashValue *val;
int rc, r;
+ if (strlen(key) >= KEYLEN_MAX)
+ return cht_staticerr(ip, "key too long", "CDB KEYOVERFLOW");
+
if (!rw->logfile) return cht_staticerr
(ip, "previous compact failed; cdbwr must be closed and reopened "
"before any further updates", "CDB BROKEN");
if (!rw->logfile) return cht_staticerr
(ip, "previous compact failed; cdbwr must be closed and reopened "
"before any further updates", "CDB BROKEN");
@@
-935,9
+941,3
@@
int cht_do_cdbwr_lookup_hb(ClientData cd, Tcl_Interp *ip, void *rw_v,
return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
cht_cdb_storeanswer_hb);
}
return cht_cdb_donesomelookup(ip, rw_v, def, result, data, dlen,
cht_cdb_storeanswer_hb);
}
-
-int cht_do_cdbtoplevel_cdb_wr(ClientData cd, Tcl_Interp *ip,
- const Cdbwr_SubCommand* subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func((void*)subcmd,ip,objc,objv);
-}