Skip to content

Commit

Permalink
Add another binbuf_write() bugfix from vanilla Pd.
Browse files Browse the repository at this point in the history
This was committed on Christmas Eve, so it must be important. :)

"fixed error message handling in binbuf_write() which accessed stale
memory" by msp (Dec 24, 2019)

See pure-data/pure-data@99fb497c
  • Loading branch information
agraef committed Sep 16, 2024
1 parent 5d6f1d9 commit 3926edc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 39 deletions.
6 changes: 5 additions & 1 deletion pd/src/g_readwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ file format as in the dialog window for data.
#include "m_pd.h"
#include "g_canvas.h"
#include <string.h>
#include <errno.h>

/* object to assist in saving state by abstractions */
static t_class *savestate_class;
Expand Down Expand Up @@ -968,7 +969,10 @@ static void canvas_savetofile(t_canvas *x, t_symbol *filename, t_symbol *dir,
t_binbuf *b = binbuf_new();
canvas_savetemplatesto(x, b, 1);
canvas_saveto(x, b);
if (binbuf_write(b, filename->s_name, dir->s_name, 0)) sys_ouch();
errno = 0;
if (binbuf_write(b, filename->s_name, dir->s_name, 0))
post("%s/%s: %s", dir->s_name, filename->s_name,
(errno ? strerror(errno) : "write failed"));
else
{
/* if not an abstraction, reset title bar and directory */
Expand Down
13 changes: 0 additions & 13 deletions pd/src/m_binbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,7 @@ int binbuf_write(t_binbuf *x, char *filename, char *dir, int crflag)
}

if (!(f = sys_fopen(fbuf, "w")))
{
//fprintf(stderr, "open: ");
sys_unixerror(fbuf);
goto fail;
}
for (ap = x->b_vec, indx = x->b_n; indx--; ap++)
{
int length;
Expand All @@ -1079,10 +1075,7 @@ int binbuf_write(t_binbuf *x, char *filename, char *dir, int crflag)
if (ep - bp < length)
{
if (fwrite(sbuf, bp-sbuf, 1, f) < 1)
{
sys_unixerror(fbuf);
goto fail;
}
bp = sbuf;
}
if ((ap->a_type == A_SEMI || ap->a_type == A_COMMA) &&
Expand All @@ -1106,17 +1099,11 @@ int binbuf_write(t_binbuf *x, char *filename, char *dir, int crflag)
}
}
if (fwrite(sbuf, bp-sbuf, 1, f) < 1)
{
sys_unixerror(fbuf);
goto fail;
}


if (fflush(f) != 0)
{
sys_unixerror(fbuf);
goto fail;
}

if (deleteit)
binbuf_free(x);
Expand Down
4 changes: 0 additions & 4 deletions pd/src/m_pd.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,6 @@ EXTERN void bug(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
EXTERN void pd_error(void *object, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
EXTERN void logpost(const void *object, const int level, const char *fmt, ...)
ATTRIBUTE_FORMAT_PRINTF(3, 4);
EXTERN void sys_logerror(const char *object, const char *s);
EXTERN void sys_unixerror(const char *object);
EXTERN void sys_ouch(void);


/* ------------ system interface routines ------------------- */
EXTERN int sys_isreadablefile(const char *name);
Expand Down
26 changes: 5 additions & 21 deletions pd/src/s_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,8 @@ void bug(const char *fmt, ...)
dopost(buf);
}

/* this isn't worked out yet. */
static const char *errobject;
static const char *errstring;

void sys_logerror(const char *object, const char *s)
{
errobject = object;
errstring = s;
}

void sys_unixerror(const char *object)
{
errobject = object;
errstring = strerror(errno);
}

void sys_ouch(void)
{
if (*errobject) error("%s: %s", errobject, errstring);
else error("%s", errstring);
}
/* don't use these. They're included for binary compatibility with
old externs but never worked and now do nothing. */
void sys_logerror(const char *object, const char *s) {}
void sys_unixerror(const char *object) {}
void sys_ouch(void) {}

0 comments on commit 3926edc

Please sign in to comment.