Commit 3ae4732a authored by Frits Daalmans's avatar Frits Daalmans

small strlen() optimization,

changed some dynamic allocations into fixed-length strings,
found 2 minor buffer overflows (if text lines in Packages run over the MAX_LENGTH=1000 bytes).
parent 068b3e03
......@@ -5,6 +5,7 @@
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#define MAX_LINE 1000
#define MAX_PKGS 100
......@@ -131,6 +132,8 @@ static void outputdeps(char *deps) {
typedef struct deps_proto {
char *cur_pkg;
int nd;
/* 95% of Depends: in jessie have less than 16 entries, for the rest
we do a realloc() when necessary. */
#define DEPCHUNK 16
char ** deps;
} deps_t, *deps_p;
......@@ -196,9 +199,9 @@ static void deps_add_4(deps_p d, const char *dep) {
}
static int deps_add_3(deps_p d, const char *versioneddep) {
char *workptr, *tok3, *saveptr3, *versionclause;
char workptr[MAX_LINE+1], *tok3, *saveptr3, *versionclause;
int used;
workptr = strdup(versioneddep);
strcpy(workptr,versioneddep);
tok3 = strtok_r(workptr, " (", &saveptr3);
versionclause = strtok_r(NULL, " (", &saveptr3);
......@@ -216,13 +219,11 @@ static int deps_add_3(deps_p d, const char *versioneddep) {
deps_add_4(d, tok3);
}
free(workptr);
return(used);
}
static void deps_add_2(deps_p d, const char *depsaltlist) {
char *workptr, *tok2, *saveptr2;
char workptr[MAX_LINE+1], *tok2, *saveptr2;
int used, chose_alternative;
/* shortcut: most dependencies don't have the alternatives syntax */
......@@ -232,7 +233,7 @@ static void deps_add_2(deps_p d, const char *depsaltlist) {
}
/* parse this dependency-alternatives-list A | B | C */
workptr = strdup(depsaltlist);
strcpy(workptr, depsaltlist);
tok2 = strtok_r(workptr, " |", &saveptr2);
chose_alternative = 0;
used = 0;
......@@ -253,22 +254,21 @@ static void deps_add_2(deps_p d, const char *depsaltlist) {
fprintf(stderr,"I: pkgdetails: used %s dependency on %s\n", d->cur_pkg, tok2);
}
}
free(workptr);
}
static void deps_add(deps_p d, const char *depslist) {
char *workptr, *tok, *saveptr = NULL;
char workptr[MAX_LINE+1], *tok, *saveptr = NULL;
/* skip initial space */
while ((depslist[0])&&(isspace(depslist[0]))) depslist++;
workptr = strdup(depslist);
assert(strlen(depslist) <= MAX_LINE);
strcpy(workptr, depslist);
tok = strtok_r(workptr, ",", &saveptr);
while(tok){
deps_add_2(d, tok);
tok = strtok_r(NULL, ",", &saveptr);
}
free(workptr);
}
static void deps_output(FILE *f, const deps_p d) {
......@@ -295,7 +295,7 @@ static void deps_output(FILE *f, const deps_p d) {
don't like the first choice of an altdep list (hint: init)
*/
static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
char buf[MAX_LINE];
char buf[MAX_LINE+1];
char cur_pkg[MAX_LINE];
char cur_deps[MAX_LINE];
char cur_predeps[MAX_LINE];
......@@ -307,6 +307,7 @@ static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
int output_pkg = -1;
deps_p curdeps = NULL;
buf[MAX_LINE+1-1] = '\0';
cur_pkg[0] = cur_deps[0] = cur_predeps[0] = prev_pkg[0] = '\0';
for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
......@@ -320,7 +321,7 @@ static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
curdeps = deps_new();
skip = 1;
while (fgets(buf, sizeof(buf), f)) {
while (fgets(buf, MAX_LINE, f)) {
l = strlen(buf);
if (*buf && buf[l-1] == '\n') buf[l-1] = '\0';
if (strncasecmp(buf, "Package:", 8) == 0) {
......@@ -369,7 +370,7 @@ static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
char *fieldname, char **in_pkgs, int pkgc)
{
char buf[MAX_LINE];
char buf[MAX_LINE+1];
char cur_field[MAX_LINE];
char cur_pkg[MAX_LINE];
char cur_ver[MAX_LINE];
......@@ -379,11 +380,12 @@ static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
char cur_filename[MAX_LINE];
char prev_pkg[MAX_LINE];
char *pkgs[MAX_PKGS];
int i;
int i,l;
FILE *f;
char *output = NULL;
int output_pkg = -1;
buf[MAX_LINE+1-1] = '\0';
cur_field[0] = cur_pkg[0] = cur_ver[0] = cur_arch[0] = cur_filename[0] = prev_pkg[0] = '\0';
for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];
......@@ -393,8 +395,9 @@ static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
perror(pkgsfile);
exit(1);
}
while (fgets(buf, sizeof(buf), f)) {
if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
while (fgets(buf, MAX_LINE, f)) {
l = strlen(buf);
if (*buf && buf[l-1] == '\n') buf[l-1] = '\0';
if (strncasecmp(buf, fieldname, strlen(fieldname)) == 0) {
fieldcpy(cur_field, buf);
}
......@@ -456,12 +459,14 @@ static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile,
static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
{
char buf[MAX_LINE];
char buf[MAX_LINE+1];
char *accum;
size_t accum_size = 0, accum_alloc = MAX_LINE * 2;
char cur_pkg[MAX_LINE];
FILE *f;
int l;
buf[MAX_LINE+1-1] = '\0';
accum = malloc(accum_alloc);
if (!accum)
oom_die();
......@@ -472,7 +477,7 @@ static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
free(accum);
exit(1);
}
while (fgets(buf, sizeof(buf), f)) {
while (fgets(buf, MAX_LINE, f)) {
if (*buf) {
size_t len = strlen(buf);
if (accum_size + len + 1 > accum_alloc) {
......@@ -484,7 +489,8 @@ static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
strcpy(accum + accum_size, buf);
accum_size += len;
}
if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
l = strlen(buf);
if (*buf && buf[l-1] == '\n') buf[l-1] = '\0';
if (strncasecmp(buf, "Package:", 8) == 0) {
fieldcpy(cur_pkg, buf);
} else if (!*buf) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment