tdb2: disallow SEED attribute with TDB_VERSION1.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 31 Aug 2011 06:01:06 +0000 (15:31 +0930)
It also only makes sense with O_CREAT.

ccan/tdb2/open.c
ccan/tdb2/test/failtest_helper.h

index 5d5eff1394b870233e90d22375438cc4be591cfc..59b4667d1bb86320ef79f226e3de609888dc6939 100644 (file)
@@ -259,6 +259,13 @@ enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
                attr->hash.data = tdb->hash_data;
                break;
        case TDB_ATTRIBUTE_SEED:
+               if (tdb->flags & TDB_VERSION1)
+                       return tdb->last_error
+                               = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                    "tdb_get_attribute:"
+                                    " cannot get TDB_ATTRIBUTE_SEED"
+                                    " on TDB1 tdb.");
                attr->seed.seed = tdb->hash_seed;
                break;
        case TDB_ATTRIBUTE_OPENHOOK:
@@ -431,6 +438,25 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                }
        }
 
+       if (seed) {
+               if (tdb_flags & TDB_VERSION1) {
+                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                          TDB_LOG_USE_ERROR,
+                                          "tdb_open:"
+                                          " cannot set TDB_ATTRIBUTE_SEED"
+                                          " on TDB1 tdb.");
+                       goto fail;
+               } else if (!(tdb_flags & TDB_INTERNAL)
+                          && !(open_flags & O_CREAT)) {
+                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                          TDB_LOG_USE_ERROR,
+                                          "tdb_open:"
+                                          " cannot set TDB_ATTRIBUTE_SEED"
+                                          " without O_CREAT.");
+                       goto fail;
+               }
+       }
+
        if ((open_flags & O_ACCMODE) == O_WRONLY) {
                ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
                                   "tdb_open: can't open tdb %s write-only",
index 37acbdd832ae2995e2fea882d630b1548a77b59a..403ff6475893f56c6872e8bab14d6dd4cba59b5c 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdbool.h>
 
 /* FIXME: Check these! */
-#define INITIAL_TDB_MALLOC     "open.c", 360, FAILTEST_MALLOC
+#define INITIAL_TDB_MALLOC     "open.c", 367, FAILTEST_MALLOC
 #define URANDOM_OPEN           "open.c", 61, FAILTEST_OPEN
 #define URANDOM_READ           "open.c", 41, FAILTEST_READ