r18311: Simplify gencache_get by using strtol instead of sscanf
authorVolker Lendecke <vlendec@samba.org>
Sat, 9 Sep 2006 21:31:56 +0000 (21:31 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:51:19 +0000 (11:51 -0500)
(This used to be commit f6497adac674f9e5089a2e54ead07596e568a936)

source3/lib/gencache.c

index a9900fd4d8cd16a4ba062dcaaf8737716031b844..871d1d1d8020ad41b4a389c5a8c02a92c57ed53c 100644 (file)
@@ -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