[Beowulf] Stupid MPI programming question

Robert G. Brown rgb at phy.duke.edu
Thu Sep 28 05:57:28 PDT 2006


On Thu, 28 Sep 2006, Jakob Oestergaard wrote:

> On Thu, Sep 28, 2006 at 01:47:17AM +0100, Clements, Brent M (SAIC) wrote:
>> Hey Guys,
> ...
>>
>> This is what I have in my code(some things have been removed)
>>
>> #define MASTER_RANK 0
>>
>> char* mystring;
>
> So mystring is a pointer to character(s).
>
>>
>> mystring = "some test";
>
> Now mystring points to a statically allocated string "some test".
>
> You must not overwrite "some test" with other data - the data area is
> assumed to be constant and compilers are even free to merge such
> strings. For example:

Ah, that's it.  I'd forgotten this one and missed the write to a static
string, although it has bitten me in the past (partly because back in
the remote K&R past one could nearly always get away with it).  This is
also a way that buffer overwrite attacks can begin if any nefarious
human can control the string that is overwritten IIRC...

Although in this particular case, that should have produced a very
different error than -1 on the mkdir call, should it not?  And he was
writing out the results string per node right before calling as well, so
his compiler was probably letting him get away with it or failing in
some odd way later.

> What you probably want to do is:
> ---
> char foo[1024];          // 1KiB on the stack - writable
> strncpy(foo, sizeof foo, "my test"); // Assign contents by copying
> ...
> foo[0] = ' ';             // <- fine
> ---

Absolutely.  Exactly the same things are true for mkstemp, which is the
"approved" way of generating unique temporary file names, by the way.

    rgb

-- 
Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb at phy.duke.edu





More information about the Beowulf mailing list