Path: utzoo!attcan!uunet!husc6!mailrus!ames!lll-tis!helios.ee.lbl.gov!ux1.lbl.gov!beard
From: beard@ux1.lbl.gov (Patrick C Beard)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Help with port to LSC of command line-driven program
Summary: Funcs[i].Name could be blowing up the function pointer!
Keywords: Quick fix before I re-write it into a real Mac program
Message-ID: <677@helios.ee.lbl.gov>
Date: 10 Aug 88 20:08:22 GMT
References: <1426@murdu.OZ>
Sender: usenet@helios.ee.lbl.gov
Reply-To: beard@ux1.lbl.gov (Patrick C Beard)
Organization: Lawrence Berkeley Laboratory, Berkeley
Lines: 74

I couldn't send this direct, using following address...
To: strasser@munnari.UUCP
Subject: Re: Help with port to LSC of command line-driven program
Newsgroups: comp.sys.mac.programmer
In-Reply-To: <1426@murdu.OZ>
Organization: Lawrence Berkeley Laboratory, Berkeley
Cc: 
Bcc: 

In article <1426@murdu.OZ> you write:
>I'm new to programming Macs and have a program in C which has run
>successfully on PCs, VAXes and a Unix machine (so I know it works). 
>...
>When I try to run the program I get nowhere because it can't even read
>the first file properly before it either stops with a spurious error or
>bombs.  I don't know enough 68k asm to understand Macsbug.
>
>The routine which bombs is being called from another through one of
>these structures:
>
>typedef struct {
>  char Name[9];
>  void (*FuncPtr)();
>} FuncVar;
>

This seems perfectly acceptable.

>(there's an array of them).  Each associates a name with a function
>pointer so that the user types the name of the function and it gets
>called, thus:
>
>      (*Funcs[i].FuncPtr)();
>

Have you thought about what might happen if a string of longer than
8 characters (with the '\0' that makes 9) was placed into Name[9]?  If
the array bounds were blown up, the FuncPtr could point to random memory.
If you reversed the order of these in the structure, you would at least
not have to worry about the function calls dying (although something
else would get screwed up!).

>(where Funcs is the aforementioned array).  
>
>Could this be the problem?  The offending function is essential before

As I said above.
>
>What about program segmentation? Do I need to have particular functions
>in the same segment? All functions pointed to in the array Funcs (and
>the calling function) are in the same segment.  Does  need to be
>there too (it's huge: 18+ K)?
>

As for segmentation, the placement of functions in various segments is
only critical for memory management, and if you refer to any function
that is in another segment, a jump table entry is generated for it
even if the function is declared static!  LSC does this and it is
quite nice.  Stdio needn't be in the same segment and the functions
pointed to by Funcs needn't be in the same segment as the calling
function.  Period.

>Can someone please give me some ideas, or tell me what other information
>I need to provide?  I have nothing to go on at the moment.  The whole
>program is c. 3000 lines of code, but I could mail bits of it if necessary.
>...
>Mike Strasser                ACSnet, CSnet:  strasser@murdu.oz
>                             Internet:       strasser%murdu.oz@uunet.uu.net

I hope this helps.

Patrick Beard
Lawrence Berkeley Laboratory
beard@ux1.lbl.gov