[sac-user] [sac-devel] filename string not passed through to writePNG(..)

Artem Shinkarov artyom.shinkaroff at gmail.com
Wed Feb 17 23:53:23 CET 2016


On 16-02-17 17:30:06, Scholz, Sven-Bodo wrote:
> In case you are not subscribed to sac-user (which you should(!) :-) here an outcry for help :-)

Also, I found the reason why writePNG misbehaves.

On 2012-03-03 Bep Rinto in the commit 632c1baee4c1d25967e5d42fae3e405d845abbc8
changed the function signature from

external int writePNG( string NAME, color[.,.] array);

to

external void writePNG( string NAME, color[.,.] array);


he updated the refcounting parameter from 2 to 1, but he didn't update
the C implementation of the writePNG, which has a signature:

void SAC_PNG_array2png(SAC_ND_PARAM_out_nodesc(res_nt, int),
                       SAC_ND_PARAM_in_nodesc(string_nt, string),
                       SAC_ND_PARAM_in(ar_nt, int))

The fun part is that it links perfectly fine, as the linker is only interested
in the name of the function and nothing else.

Searching for such bugs is INCREDIBLY annoying.  How about generating a C
declaration for every external function and use this declaration in C
files?  In that case a C compiler will notice mismatch in arguments and
will complain.



Cheers,
Artem



> 
> Bodo
> 
> > On 16 Feb 2016, at 23:53, Rob Stewart <R.Stewart at hw.ac.uk> wrote:
> > 
> > I have some code that:
> > 
> > use PNG: {writePNG};
> > 
> > And
> > 
> > call writePNG("foo.png", anArray);
> > 
> > When I compile and run this, I am getting the runtime error:
> > 
> > *** SAC runtime error
> > *** SAC_PNG_array2png: Failed to open image file '': No such file or directory
> > 
> > My "foo.png" doesn't look to be carried through correctly to
> > writePNG(..).
> > 
> > In the stdlib/src/world/stdio/PNG.sac
> > 
> > --8<---------------cut here---------------start------------->8---
> > external void writePNG( string NAME, color[.,.] array);
> >    #pragma effect FileSystem::TheFileSystem
> >    #pragma linkobj "src/PNG/array2png.o"
> >    #pragma linkname "SAC_PNG_array2png"
> >    #pragma linkwith "png"
> >    #pragma linkwith "z"
> >    #pragma refcounting[1]
> > --8<---------------cut here---------------end--------------->8---
> > 
> > And in stdlib/src/world/stdio/src/PNG/array2png.c I get this far:
> > 
> > --8<---------------cut here---------------start------------->8---
> > void SAC_PNG_array2png(SAC_ND_PARAM_out_nodesc(res_nt, int),
> >                       SAC_ND_PARAM_in_nodesc(string_nt, string),
> >                       SAC_ND_PARAM_in(ar_nt, int))
> > {
> >  FILE *fp;
> >  png_structp png_ptr;
> >  png_infop info_ptr;
> >  png_bytep *row_pointers;
> >  png_bytep img_data;
> >  int x, y;
> >  int height = SAC_ND_A_DESC_SHAPE(ar_nt, 0);
> >  int width = SAC_ND_A_DESC_SHAPE(ar_nt, 1);
> >  int *arr_data = SAC_ND_A_FIELD(ar_nt);
> > 
> >  fp = fopen(NT_NAME(string_nt), "wb");
> >  if (fp == NULL)
> >  {
> >    SAC_RuntimeError("SAC_PNG_array2png: Failed to open image file '%s': %s",
> >                     NT_NAME(string_nt), strerror(errno));
> >    return;
> >  }
> > --8<---------------cut here---------------end--------------->8---
> > 
> > 
> > Why does NT_NAME(string_nt) return an empty string, and what can I do
> > about it? Or am I missing something completely obvious?
> > 
> > Thanks,
> > 
> > --
> > Rob
> > _______________________________________________
> > sac-user mailing list
> > sac-user at sac-home.org
> > http://lists.sac-home.org/mailman/listinfo/sac-user



More information about the sac-user mailing list