termination of strings in solaris 9

Hi,

I have quite a bit of code that has been working for some time under solaris 8. When upgrading to Solaris 9 ( sparc) we ran into problem with

strings NOT terminated with NULL. Earlier it was possible to get the compiler to accept that and instead space-fill up to to the declared length. I have tried almost every flag in the c-compiler ( Studio 11) but not been lucky to get it OK. Hopefully someone has been more persistent than I ( or got more luck).

/Lars Wallin

[495 byte] By [lasse50] at [2007-11-26 8:18:30]
# 1
I cannot tell from your description what you are trying to do. Please post a code example that does not do what you want.
clamage45 at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

Hi , the routine is quite small and are using a special library to handle the informix database. Part of the code is as follows:

1)

if( op == '1' )

cc = iod("C1,GF","si002",&si002buf,"*","F,loginid,EQ",&neclogin,"Q");

else

[b] cc = iod("C2,GF","si002",&si002buf,"*","F,loginid,EQ",&neclogin,"Q");

if ( cc != IODOK )

{

fprintf(err,"ERROR: Hittade inte Login: %s, cc = %d\n",login,cc);

cc = iod("EV,ET","*");

znsysdel();

printf("-1");

return( -1);

}

fill (pass,sizeof pass,NULL);

fill (necpass,sizeof necpass,NULL);

movst(argv[2],0,pass,0,v2l);

decryptSJ(pass, v2l);

movst(si002buf.pass,0,necpass,0,v2l);

zndecry(necpass, strlen(necpass));

fill(fillpass, sizeof fillpass,NULL);

movst(pass,0,fillpass,0,strlen(pass));

if( znstcp(necpass,fillpass,znlength(necpass, strlen(necpass))))

{

fprintf(err,"ERROR: L鰏enorden 鋜 fel.\n");

cc = iod("EV,ET","*");

znsysdel();

printf("-2");

return( -2);

}

fill(dbbeh,sizeof dbbeh,NULL);

movst(si002buf.sit004,0,dbbeh,0,8);

fprintf(err,"Test av NULL i -xc99=nOne -xchar=u & Xt: %s %d \n",si002buf.sit

004,strlen(si002buf.sit004));

movst(dbbeh,0,beh,0,8);

if(znstcp("sisujava",beh,8))

- --

2) The structure of the database record:

sisu@hagbard$ cat si002.sfi

struct SI002P/* autogenerated 2006-06-27 11:13:20 */

{

unsigned char loginid[12];

unsigned char sign[8];

unsigned char pass[10];

double datchgd;

int sit011;

int knr;

unsigned char sit012[1];

int sit001;

int sit002;

int sit003;

unsigned char sit004[8];

unsigned char sit005[8];

unsigned char sit006[8];

unsigned char sit007[8];

unsigned char sit008[8];

unsigned char sit009[8];

};

=====================================================

3) The result when running the routine:

sisu@hagbard$ cat AuthChk*

AuthChk.bin startad : 06-06-27 11:23:39

Parametrarna 鋜 (rgpq ),(rgpq ),(null) och (0)

L鋘gder: 4 4

znstart OK!

znappin sisu98 OK!

OP si002 OK!

Login innan konv: (rgpq)

Login efter konv: (sisu)

Test av NULL i -xc99=nOne -xchar=u & Xt: sisujavaALL

48

OK, Operation Successful!

======================================================

It looks as if "strlen" think that the string is 48 Chars, when it is only 8 chars when it's not terminated by NULL.

--

Hope this will give enough information. Otherwise pse come back for more.

Lasse50

lasse50 at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

If a string is not terminated with 0, strlen() continues searching for this zero past string bounds and only stops when it finds one (but noone can tell in advance where it would be).

In order to catch this type of illegal memory access, you can use dbx, the Sun Studio debugger, run-time checking feature (check -access command) available on Sparc processors.

MaximKartashev at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4

Hi, I'am aware of the way the problem behaves but my real question was if there is a flag that can go around the problem. The c-compiler we used in solaris 8 didn't behave (produce a code) like this.

What we would like is "look for NULL up to the end of the declared string-length if not found then string-length = declared length".

Are there a compilerflag like this or is it possible to get the old (or find another) compiler-version that we can use instead.

lasse50 at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 5

No, I'm not aware of such a flag. I think the difference you see when you upgraded to Solaris 9 originates from structure alignment: in previous version, there was a space (apparently filled with zeroes) between structure fields. Now either zeroes are gone or structure is more tightly packed.

MaximKartashev at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 6

> The c-compiler we used in solaris 8 didn't behave (produce a code) like this.

Any C compiler conforming to any C standard just cant treat [b]string[/b] differently. strlen is a function from C standard library, its behavior is well-defined and in C99 is formulated this way:

"The strlen function returns the number of characters that precede the terminating NULL character".

You see, there is no mention of [i]"declared length"[/i].

You have been getting undefined behavior, probably due to the circumstances pointed out by Maxim.

regards,

__Fedor.

SFV at 2007-7-6 21:20:36 > top of Java-index,Development Tools,Solaris and Linux Development Tools...