- beautify a bit
authorMichael Schroeder <mls@suse.de>
Thu, 6 Mar 2008 19:10:24 +0000 (19:10 +0000)
committerMichael Schroeder <mls@suse.de>
Thu, 6 Mar 2008 19:10:24 +0000 (19:10 +0000)
- more overflow checks in repo_solv
- add localpool parameter

src/repo.c
src/repo.h
src/repo_solv.c
src/repodata.c

index 45fb428..f6b1e79 100644 (file)
@@ -849,19 +849,19 @@ repo_lookup(Solvable *s, Id key, int (*callback)(void *cbdata, Solvable *s, Repo
 /***********************************************************************/
 
 Repodata *
-repo_add_repodata(Repo *repo)
+repo_add_repodata(Repo *repo, int localpool)
 {
   Repodata *data;
 
   repo->nrepodata++;
   repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
   data = repo->repodata + repo->nrepodata - 1;
-  repodata_init(data, repo, 0);
+  repodata_init(data, repo, localpool);
   return data;
 }
 
 static Repodata *
-findrepodata(Repo *repo, Id p, Id keyname)
+repo_findrepodata(Repo *repo, Id p, Id keyname)
 {
   int i;
   Repodata *data;
@@ -878,34 +878,34 @@ findrepodata(Repo *repo, Id p, Id keyname)
       repodata_extend(data, p);
       return data;
     }
-  return repo_add_repodata(repo);
+  return repo_add_repodata(repo, 0);
 }
 
 void
 repo_set_id(Repo *repo, Id p, Id keyname, Id id)
 {
-  Repodata *data = findrepodata(repo, p, keyname);
+  Repodata *data = repo_findrepodata(repo, p, keyname);
   repodata_set_id(data, p - data->start, keyname, id);
 }
 
 void
 repo_set_num(Repo *repo, Id p, Id keyname, Id num)
 {
-  Repodata *data = findrepodata(repo, p, keyname);
+  Repodata *data = repo_findrepodata(repo, p, keyname);
   repodata_set_num(data, p - data->start, keyname, num);
 }
 
 void
 repo_set_str(Repo *repo, Id p, Id keyname, const char *str)
 {
-  Repodata *data = findrepodata(repo, p, keyname);
+  Repodata *data = repo_findrepodata(repo, p, keyname);
   repodata_set_str(data, p - data->start, keyname, str);
 }
 
 void
 repo_set_poolstr(Repo *repo, Id p, Id keyname, const char *str)
 {
-  Repodata *data = findrepodata(repo, p, keyname);
+  Repodata *data = repo_findrepodata(repo, p, keyname);
   repodata_set_poolstr(data, p - data->start, keyname, str);
 }
 
index b7b9549..fc78dd1 100644 (file)
@@ -159,7 +159,8 @@ typedef struct _KeyValue {
 /* Internal */
 #define __SEARCH_ONESOLVABLE           (1 << 31)
 
-Repodata *repo_add_repodata(Repo *repo);
+Repodata *repo_add_repodata(Repo *repo, int localpool);
+
 void repo_search(Repo *repo, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata);
 
 /* returns the string value of the attribute, or NULL if not found */
index 361097b..64ade8d 100644 (file)
@@ -797,6 +797,8 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
       char *pp = prefix;
       char *old_str = 0;
       char *dest = strsp;
+      int freesp = sizeid;
+
       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
         {
          pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
@@ -806,7 +808,14 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
       for (i = 1; i < numid; i++)
         {
          int same = (unsigned char)*pp++;
-         size_t len = strlen (pp) + 1;
+         size_t len = strlen(pp) + 1;
+         freesp -= same + len;
+         if (freesp < 0)
+           {
+             pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
+             sat_free(prefix);
+             return SOLV_ERROR_OVERFLOW;
+           }
          if (same)
            memcpy(dest, old_str, same);
          memcpy(dest + same, pp, len);
@@ -815,6 +824,11 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
          dest += same + len;
        }
       sat_free(prefix);
+      if (freesp != 0)
+       {
+         pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
+         return SOLV_ERROR_CORRUPT;
+       }
     }
   strsp[sizeid] = 0;                  /* make string space \0 terminated */
   sp = strsp;
@@ -888,7 +902,7 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
            {
              sat_free(hashtbl);
              sat_free(idmap);
-             pool_debug(pool, SAT_ERROR, "not enough strings\n");
+             pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
              return SOLV_ERROR_OVERFLOW;
            }
          if (!*sp)                            /* empty string */
index 037cf36..214ec1d 100644 (file)
@@ -37,6 +37,27 @@ extern unsigned int unchecked_decompress_buf (const unsigned char *in,
 
 #define REPODATA_BLOCK 255
 
+
+void
+repodata_init(Repodata *data, Repo *repo, int localpool)
+{
+  memset(data, 0, sizeof (*data));
+  data->repo = repo;
+  data->localpool = localpool;
+  if (localpool)
+    stringpool_init_empty(&data->spool);
+  data->keys = sat_calloc(1, sizeof(Repokey));
+  data->nkeys = 1;
+  data->schemata = sat_calloc(1, sizeof(Id));
+  data->schemadata = sat_calloc(1, sizeof(Id));
+  data->nschemata = 1;
+  data->schemadatalen = 1;
+  data->start = repo->start;
+  data->end = repo->end;
+  data->incoreoffset = sat_extend_resize(0, data->end - data->start, sizeof(Id), REPODATA_BLOCK);
+  data->pagefd = -1;
+}
+
 void
 repodata_free(Repodata *data)
 {
@@ -72,14 +93,14 @@ repodata_free(Repodata *data)
 }
 
 static unsigned char *
-forward_to_key(Repodata *data, Id key, Id schemaid, unsigned char *dp)
+forward_to_key(Repodata *data, Id keyid, Id schemaid, unsigned char *dp)
 {
   Id k, *keyp;
 
   keyp = data->schemadata + data->schemata[schemaid];
   while ((k = *keyp++) != 0)
     {
-      if (k == key)
+      if (k == keyid)
        return dp;
       if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
        {
@@ -260,7 +281,7 @@ static unsigned char *
 make_vertical_available(Repodata *data, Repokey *key, Id off, Id len)
 {
   unsigned char *dp;
-  if (key->type == REPOKEY_TYPE_VOID)
+  if (!len)
     return 0;
   if (off >= data->lastverticaloffset)
     {
@@ -273,6 +294,7 @@ make_vertical_available(Repodata *data, Repokey *key, Id off, Id len)
     return 0;
   /* we now have the offset, go into vertical */
   off += data->verticaloffset[key - data->keys];
+  /* fprintf(stderr, "key %d page %d\n", key->name, off / BLOB_PAGESIZE); */
   dp = load_page_range(data, off / BLOB_PAGESIZE, (off + len - 1) / BLOB_PAGESIZE);
   if (dp)
     dp += off % BLOB_PAGESIZE;
@@ -807,26 +829,6 @@ weg2:
   return 1;
 }
 
-void
-repodata_init(Repodata *data, Repo *repo, int localpool)
-{
-  memset(data, 0, sizeof (*data));
-  data->repo = repo;
-  data->localpool = localpool;
-  if (localpool)
-    stringpool_init_empty(&data->spool);
-  data->keys = sat_calloc(1, sizeof(Repokey));
-  data->nkeys = 1;
-  data->schemata = sat_calloc(1, sizeof(Id));
-  data->schemadata = sat_calloc(1, sizeof(Id));
-  data->nschemata = 1;
-  data->schemadatalen = 1;
-  data->start = repo->start;
-  data->end = repo->end;
-  data->incoreoffset = sat_extend_resize(0, data->end - data->start, sizeof(Id), REPODATA_BLOCK);
-  data->pagefd = -1;
-}
-
 /* extend repodata so that it includes solvables p */
 void
 repodata_extend(Repodata *data, Id p)
@@ -881,6 +883,8 @@ repodata_extend_block(Repodata *data, Id start, Id num)
     repodata_extend(data, start + num - 1);
 }
 
+/**********************************************************************/
+
 #define REPODATA_ATTRS_BLOCK 63
 #define REPODATA_ATTRDATA_BLOCK 1023
 #define REPODATA_ATTRIDDATA_BLOCK 63