diff options
author | Volker Lendecke <vlendec@samba.org> | 2006-09-09 21:31:56 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 11:51:19 -0500 |
commit | cfefd8fd789b6512cad418a791c274faf54f11da (patch) | |
tree | 401bf49ee4335c413551840065347ac7a0c7013c | |
parent | 96c72e2f8152f2b9e006a392e6e8d1006b9cdd2c (diff) | |
download | samba-cfefd8fd789b6512cad418a791c274faf54f11da.tar.gz samba-cfefd8fd789b6512cad418a791c274faf54f11da.tar.bz2 samba-cfefd8fd789b6512cad418a791c274faf54f11da.zip |
r18311: Simplify gencache_get by using strtol instead of sscanf
(This used to be commit f6497adac674f9e5089a2e54ead07596e568a936)
-rw-r--r-- | source3/lib/gencache.c | 78 |
1 files changed, 28 insertions, 50 deletions
diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a9900fd4d8..871d1d1d80 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -180,6 +180,8 @@ BOOL gencache_del(const char *keystr) BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) { TDB_DATA keybuf, databuf; + time_t t; + char *endptr; /* fail completely if get null pointers passed */ SMB_ASSERT(keystr); @@ -191,67 +193,43 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); - - if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { - char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize); - char *v; - time_t t; - unsigned u; - int status; - char *fmt; - - v = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); - if (!v) { - return False; - } - SAFE_FREE(databuf.dptr); - - asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); - if (!fmt) { - SAFE_FREE(v); - return False; - } - - status = sscanf(entry_buf, fmt, &u, v); - SAFE_FREE(fmt); + if (databuf.dptr == NULL) { + DEBUG(10, ("Cache entry with key = %s couldn't be found\n", + keystr)); + return False; + } - if ( status != 2 ) { - DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status )); - } - t = u; - SAFE_FREE(entry_buf); + t = strtol(databuf.dptr, &endptr, 10); - DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " - "timeout = %s", t > time(NULL) ? "valid" : - "expired", keystr, v, ctime(&t))); + if ((endptr == NULL) || (*endptr != '/')) { + DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr)); + SAFE_FREE(databuf.dptr); + return False; + } - if (valstr) { - *valstr = v; - } else { - SAFE_FREE(v); - } + DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " + "timeout = %s", t > time(NULL) ? "valid" : + "expired", keystr, endptr+1, ctime(&t))); - if (timeout) { - *timeout = t; + if (valstr) { + *valstr = SMB_STRDUP(endptr+1); + if (*valstr == NULL) { + SAFE_FREE(databuf.dptr); + DEBUG(0, ("strdup failed\n")); + return False; } - - return t > time(NULL); - - } - + } + SAFE_FREE(databuf.dptr); - if (valstr) { - *valstr = NULL; - } if (timeout) { - timeout = NULL; + *timeout = t; } - DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); - return False; -} + return t > time(NULL); +} + /** * Iterate through all entries which key matches to specified pattern |