Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitise global data allocation; fix a void return #8

Merged
merged 10 commits into from Oct 14, 2017
21 changes: 21 additions & 0 deletions .travis.yml
@@ -0,0 +1,21 @@
language: c

matrix:
allow_failures:
- os: osx
include:
- os: osx
compiler: clang
- os: linux
compiler: gcc
addons:
apt:
packages:
- libgc-dev

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install bdw-gc ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libgc-dev ; fi

script: autoreconf --install && automake --add-missing --copy && ./configure && make check
26 changes: 13 additions & 13 deletions lib/bound.c
Expand Up @@ -21,21 +21,21 @@ A step is either adding a crossing, adding a crossing and removing a pair of
boundary crossings, or just removing a pair of boundary crossings.
------------------------------------------------------------------------------
*/
word list[BIGWEAVE]; /* description of first new weave */
word list2[BIGWEAVE]; /* description of second, if needed */
word old_going_in[BIGWEAVE];/* Was *i* an input? *old_going_in[i]*. */
word going_in[BIGWEAVE]; /* Will *i* be an input? *going_in[i]*. */
word map[BIGWEAVE]; /* i of old weave becomes map[i] of new weave */
word first; /* first boundary crossing to remove */
word second; /* second boundary crossing to remove */
word right; /* Is the crossing being added be righthanded? */
word oldcross; /* number of boundary crossings in the old weave */
word newcross; /* number of boundary crossings in each new weave */
word oldin; /* number of inputs to the old weave */
word newin; /* number of inputs to the new weave */

void b_manip(weave *oldweaves)
{
extern word list[BIGWEAVE]; /* description of first new weave */
extern word list2[BIGWEAVE]; /* description of second, if needed */
extern word old_going_in[BIGWEAVE];/* Was *i* an input? *old_going_in[i]*. */
extern word going_in[BIGWEAVE]; /* Will *i* be an input? *going_in[i]*. */
extern word map[BIGWEAVE]; /* i of old weave becomes map[i] of new weave */
extern word first; /* first boundary crossing to remove */
extern word second; /* second boundary crossing to remove */
extern word right; /* Is the crossing being added be righthanded? */
extern word oldcross; /* number of boundary crossings in the old weave */
extern word newcross; /* number of boundary crossings in each new weave */
extern word oldin; /* number of inputs to the old weave */
extern word newin; /* number of inputs to the new weave */

word i, j, k;
ub4 boundary[2];

Expand Down
26 changes: 13 additions & 13 deletions lib/bound.h
Expand Up @@ -44,21 +44,21 @@ typedef struct weave weave;

/*
---------------------------------------------------------------------------
Global Variables
Global Variables -- located in bound.c
---------------------------------------------------------------------------
*/
word list[BIGWEAVE]; /* description of first new weave */
word list2[BIGWEAVE]; /* description of second, if needed */
word old_going_in[BIGWEAVE]; /* Was *i* an input? *old_going_in[i]*. */
word going_in[BIGWEAVE]; /* Will *i* be an input? *going_in[i]*. */
word map[BIGWEAVE]; /* i of old weave becomes map[i] of new weave */
word first; /* first boundary crossing to remove */
word second; /* second boundary crossing to remove */
word right; /* Is the crossing being added righthanded? */
word oldcross; /* number of boundary crossings in the old weave */
word newcross; /* number of boundary crossings in each new weave */
word oldin; /* number of inputs to the old weave */
word newin; /* number of inputs to the new weave */
extern word list[]; /* description of first new weave */
extern word list2[]; /* description of second, if needed */
extern word old_going_in[]; /* Was *i* an input? *old_going_in[i]*. */
extern word going_in[]; /* Will *i* be an input? *going_in[i]*. */
extern word map[]; /* i of old weave becomes map[i] of new weave */
extern word first; /* first boundary crossing to remove */
extern word second; /* second boundary crossing to remove */
extern word right; /* Is the crossing being added righthanded? */
extern word oldcross; /* number of boundary crossings in the old weave */
extern word newcross; /* number of boundary crossings in each new weave */
extern word oldin; /* number of inputs to the old weave */
extern word newin; /* number of inputs to the new weave */

/*
---------------------------------------------------------------------------
Expand Down
14 changes: 8 additions & 6 deletions lib/control.c
Expand Up @@ -15,15 +15,17 @@ Public Domain
#include "model.h"
#include "control.h"

Instruct plan;
Poly llplus;
Poly lplusm;
Poly lminusm;
Poly llminus;
Poly mll;
ub4 total_weaves_handled;

/* these variables are global, and may be because nobody ever changes them */
void c_init()
{
extern Poly llplus;
extern Poly lplusm;
extern Poly lminusm;
extern Poly llminus;
extern Poly mll;
extern ub4 total_weaves_handled;
Poly blank[1];

p_init(blank);
Expand Down
18 changes: 9 additions & 9 deletions lib/control.h
Expand Up @@ -11,15 +11,15 @@
#include "poly.h"
#include "order.h"

/* Global variables */

Instruct plan; /* plan of what to do to each old weave for this step */
Poly llplus; /* -L^2 */
Poly lplusm; /* -LM */
Poly lminusm; /* -(L^-1)M */
Poly llminus; /* -L^-2 */
Poly mll; /* (M^-1)(-(L^-1) - L) */
ub4 total_weaves_handled; /* how much work has been done? */
/* Global variables -- located in control.c */

extern Instruct plan; /* plan of what to do to each old weave for this step */
extern Poly llplus; /* -L^2 */
extern Poly lplusm; /* -LM */
extern Poly lminusm; /* -(L^-1)M */
extern Poly llminus; /* -L^-2 */
extern Poly mll; /* (M^-1)(-(L^-1) - L) */
extern ub4 total_weaves_handled; /* how much work has been done? */

/*
------------------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions lib/knot.c
Expand Up @@ -70,7 +70,7 @@ void k_show(Link *link)
Assumes the file given by the user exists and contains a legal knot.
------------------------------------------------------------------------------
*/
boolean k_read(Link **link, char *filename)
boolean k_read(Link **link, char *f)
{
char name[20];
word links,
Expand All @@ -80,31 +80,32 @@ boolean k_read(Link **link, char *filename)
over,
i,
j;
int num_crossings;
int num_crossings, pos;
crossing *kk;
crossing k[MAXCROSS];
FILE *f;

for (i = 0; i < MAXCROSS; i++) k[i].hand = 0;

f = fmemopen(filename, strlen(filename), "r");
if (f == 0)
{
fclose(f);
return FALSE;
}
fscanf(f, "%d ", &links);
sscanf(f, "%d%n", &links, &pos);
f += pos;
for (i=0; i<links; ++i) /* how many pieces of string */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of updating the position of the f pointer could have side effects. Wouldn't it be safer to use a copy instead of the pointer passed to the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a call by value. Nothing will happen to f outside the scope of k_read(). (to change f inside k_read(), you'd need to pass &f, i.e. its address).

{
fscanf(f, "%d ", &num_crossings);
fscanf(f, "%d %d ", &startwhere, &startover);
sscanf(f, "%d%n", &num_crossings, &pos);
f += pos;
sscanf(f, "%d %d%n", &startwhere, &startover, &pos);
f += pos;
if (startover == 1)
l_add((dllink *)0, startwhere, &k[startwhere].o);
else
l_add((dllink *)0, startwhere, &k[startwhere].u);
for (j=1; j<num_crossings; ++j)
{
fscanf(f, "%d %d ", &where, &over);
sscanf(f, "%d %d%n", &where, &over, &pos);
f += pos;

/* check that OVER is legal */
if ((over != 1) && (over != -1))
Expand All @@ -130,13 +131,13 @@ boolean k_read(Link **link, char *filename)
}
}
num_crossings = 0;
while (fscanf(f, "%d %d ", &where, &over) == 2)
while (sscanf(f, "%d %d%n", &where, &over, &pos) == 2)
{
f += pos;
k[where].hand = over;
if (where > num_crossings) num_crossings = where;
}
++num_crossings;
i = fclose(f);

kk = (crossing *)GC_MALLOC(sizeof(crossing) * num_crossings);
for (i=0; i < num_crossings; ++i) kk[i] = k[i];
Expand Down
42 changes: 25 additions & 17 deletions lib/poly.c
Expand Up @@ -68,40 +68,48 @@ char *p_show(Poly *p)
sb4 l;
char *bp;
size_t size;
FILE *stream;


stream = open_memstream(&bp, &size);
int pos, offs, maxlen;
#define bp_chunksize 10000
#define bp_chunkreserve 500
#define bp_sprintf(...) {\
sprintf(bp+offs, __VA_ARGS__, &pos);\
offs += pos;\
if (offs > maxlen - bp_chunkreserve) {\
maxlen += bp_chunksize + bp_chunkreserve;\
bp = (char *)GC_REALLOC(bp, sizeof(char) * maxlen);\
}\
}
maxlen = bp_chunksize + bp_chunkreserve;
offs = 0;
bp = (char *)GC_MALLOC(sizeof(char) * maxlen);
if (!p->len)
{
fprintf(stream, "0");
fclose(stream);
return;
bp_sprintf("0%n")
return bp;
}

for (first = 1, pt = p->term, pend = pt+p->len; pt != pend; ++pt)
{
if (!pt->coef) continue;
m = p_sign(pt->m);
l = p_sign(pt->l);
if (pt->coef < ((sb4)0)) fprintf(stream, " - ");
else if (!first) fprintf(stream, " + ");
if (pt->coef > ((sb4)1) ) fprintf(stream, "%ld", pt->coef);
else if (pt->coef < (sb4)(-1)) fprintf(stream, "%ld", -pt->coef);
else if ((!l) && (!m)) fprintf(stream, "%d", 1);
if (pt->coef < ((sb4)0)) bp_sprintf(" - %n")
else if (!first) bp_sprintf(" + %n")
if (pt->coef > ((sb4)1) ) bp_sprintf("%ld%n", pt->coef)
else if (pt->coef < (sb4)(-1)) bp_sprintf("%ld%n", -pt->coef)
else if ((!l) && (!m)) bp_sprintf("%d%n", 1)
if (pt->m)
{
fprintf(stream, "M");
if (m != 1) fprintf(stream, "^%ld", m);
bp_sprintf("M%n")
if (m != 1) bp_sprintf("^%ld%n", m)
}
if (pt->l)
{
fprintf(stream, "L");
if (l != 1) fprintf(stream, "^%ld", l);
bp_sprintf("L%n")
if (l != 1) bp_sprintf("^%ld%n", l)
}
if (first) first = 0;
}
fclose(stream);
return bp;
}

Expand Down