Operating System - HP-UX
1751944 Members
4832 Online
108783 Solutions
New Discussion юеВ

Re: iconv cause core dump

 
yuyuguang
Occasional Advisor

iconv cause core dump

I have function convertя╝Иargs...)to convert hp15CN to utf8

In a simple program, it works fine.
#include
#include
#include
#include
int Convert(char* encFrom, char* encTo, const char* in, int in_len, char* out, int out_len)
{
char* sin, *sout;
int ret;
iconv_t c_pt;
int ilen, olen;


printf("1\n");
if((c_pt = iconv_open(encTo, encFrom)) == (iconv_t)-1)
{
printf("c_pt == -1\n");
return -1;
}
iconv(c_pt, NULL, NULL, NULL, NULL);
sin = (char*)in;
sout = (char*)out;
ilen = in_len;
olen = out_len;

printf("2\n");
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);
printf("3\n");
if(ret == -1)
{
iconv_close(c_pt);
printf("iconv error\n");
return -1;
}
iconv_close(c_pt);
return 1;
}

int main()
{
char buf[50];

char* str = " ???/PLASTIC SHEET (5M)";
memset(buf, '\0', sizeof(buf));
Convert("hp15CN", "utf8", str, strlen(str), buf, sizeof(buf));
printf("buf = %s\n", buf);
}

output:
1
2
3
buf = ?PLASTIC SHEET (5M)


but the functioon in my another program(run in the same machine), it caursed core.


see the attached files.

Anybody who can help me?
8 REPLIES 8
yuyuguang
Occasional Advisor

Re: iconv cause core dump

but the functioon in my another program(run in the same machine), it caursed core.


int Convert(char* encFrom, char* encTo, const char* in, int in_len, char* out, int out_len)
{
char* sin, *sout;
int ret;
iconv_t c_pt;
int ilen, olen;


printf("1\n");
if((c_pt = iconv_open(encTo, encFrom)) == (iconv_t)-1)
{
printf("c_pt == -1\n");
return -1;
}
iconv(c_pt, NULL, NULL, NULL, NULL);
sin = (char*)in;
sout = (char*)out;
ilen = in_len;
olen = out_len;

printf("2\n");
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);
printf("3\n");
if(ret == -1)
{
iconv_close(c_pt);
printf("iconv error\n");
return -1;
}
iconv_close(c_pt);
return 1;
}

Invorking the Convert function
ret = Convert("hp15CN", "utf8", fli_sup->supplies_desc, strlen(fli_sup->supplies_desc)+1, temp_buf, sizeof(temp_buf)-
1);
if(ret < 0)
{
xmlFreeDoc(doc);
return GBK_TO_UTF8_ERROR;
}

output:
sup_desc1=???/PLASTIC SHEET (5M), sup_desc2=???/PLASTIC SHEET (5M)
test 1
1
2
3
sin=adfasdf, sout=, ilen=7, olen=50
Bus error(coredump)
yuyuguang
Occasional Advisor

Re: iconv cause core dump

Following are gdb messages
Detected 64-bit executable.
Invoking /opt/langtools/bin/gdb64.
HP gdb 3.2 for PA-RISC 2.0 (wide), HP-UX 11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 3.2 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
..(no debugging symbols found)...
Core was generated by `exdatass'.
Program terminated with signal 10, Bus error.
(no debugging symbols found)...
warning: The shared libraries were not privately mapped; setting a
breakpoint in a shared library will not work until you rerun the program.

(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
warning: section .data not found in /oradata/orabase/product/9.2.0/lib/libwtc9.sl
(no debugging symbols found)...
(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
#0 0xc00000000004d898 in _instantiate_iconv+0x2f8 () from /usr/lib/nls/iconv/pa20_64/methods.2/lhp15utf8.sl
(gdb) where
#0 0xc00000000004d898 in _instantiate_iconv+0x2f8 () from /usr/lib/nls/iconv/pa20_64/methods.2/lhp15utf8.sl
#1 0xc000000000315200 in iconv+0x30 () from /usr/lib/pa20_64/libc.2
#2 0x400000000001920c in Convert1+0x244 ()
#3 0x400000000001d768 in ProcessFlightSupply+0x358 ()
#4 0x400000000000e7d4 in GetFlightSupplies+0x12fc ()
#5 0x400000000001855c in main+0x11b4 ()
(gdb) quit



machine info:
uname -a
HP-UX training B.11.11 U 9000/800 3112192028 unlimited-user license

It's a strange problem.
yuyuguang
Occasional Advisor

Re: iconv cause core dump

How can i solve this problem?
Dennis Handly
Acclaimed Contributor

Re: iconv cause core dump

>In a simple program, it works fine.

You neglected to mention you compiled with +DD64. Doing this causes an abort.

>char* str = " ???/PLASTIC SHEET (5M)";

This code is illegal. You need to quote the bogus trigraph:
char *str = " ??\?/PLASTIC SHEET (5M)";

Using a real compiler with -z +DD64 +wlint +w64bit you get these errors:
warning #4228-D: 64 bit migration: conversion from "int *" to a more strictly aligned type "size_t *" may cause misaligned access
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);
warning #4228-D: 64 bit migration: conversion from "int *" to a more strictly aligned type "size_t *" may cause misaligned access
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);

This is your problem. in_len and out_len must be size_t.

>iconv(c_pt, NULL, NULL, NULL, NULL);

You can't do this. You get a signal 11.
yuyuguang
Occasional Advisor

Re: iconv cause core dump

Hi Dennis Handly, you are right.

see the output:
training:/cosys06/cosys/edi/test>make -f test_iconv.mk test_iconv
/usr/ccs/bin/make -f /cosys06/cosys/edi/test/test_iconv.mk OBJS="test_iconv.o /cosys06/cosys/edi/src/interface/shared/edinutil.o /cosys06/cosys/edi/src/interface/shared/edutlog.o " EXE=/cosys06/cosys/edi/test/test_iconv build LIBHOME="/oradata/orabase/product/9.2.0/lib/" LIBXML="-L/usr/local/lib -lxml2 -lpthread -lm" PRODLIBHOME="/oradata/orabase/product/9.2.0/precomp/lib" CFLAGS="+DA2.0W +DS2.0 +DA2.0W +DS2.0 -DSS_64BIT_SERVER -I. -I/oradata/orabase/product/9.2.0/precomp/public -I/oradata/orabase/product/9.2.0/rdbms/public -I/oradata/orabase/product/9.2.0/rdbms/demo -I/oradata/orabase/product/9.2.0/plsql/public -I/oradata/orabase/product/9.2.0/network/public " LFLAGS="+DA2.0W"
cc +DA2.0W +DS2.0 +DA2.0W +DS2.0 -DSS_64BIT_SERVER -I. -I/oradata/orabase/product/9.2.0/precomp/public -I/oradata/orabase/product/9.2.0/rdbms/public -I/oradata/orabase/product/9.2.0/rdbms/demo -I/oradata/orabase/product/9.2.0/plsql/public -I/oradata/orabase/product/9.2.0/network/public -I/cosys06/cosys/edi/src/shared/include/ -I/usr/local/include/libxml2/ -o test_iconv.o -c test_iconv.c
cc +DA2.0W -o /cosys06/cosys/edi/test/test_iconv test_iconv.o /cosys06/cosys/edi/src/interface/shared/edinutil.o /cosys06/cosys/edi/src/interface/shared/edutlog.o -L/oradata/orabase/product/9.2.0/lib/ -lclntsh `cat /oradata/orabase/product/9.2.0/lib/ldflags` `cat /oradata/orabase/product/9.2.0/lib/sysliblist` -lm -lpthread -lpthread -L/usr/local/lib -lxml2 -lpthread -lm
training:/cosys06/cosys/edi/test>./test_iconv
1
2
Bus error(coredump)
training:/cosys06/cosys/edi/test>gdb test_iconv core
Detected 64-bit executable.
Invoking /opt/langtools/bin/gdb64.
HP gdb 3.2 for PA-RISC 2.0 (wide), HP-UX 11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 3.2 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
..(no debugging symbols found)...
Core was generated by `test_iconv'.
Program terminated with signal 10, Bus error.
(no debugging symbols found)...
warning: The shared libraries were not privately mapped; setting a
breakpoint in a shared library will not work until you rerun the program.

(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...(no debugging symbols found)...
(no debugging symbols found)...(no debugging symbols found)...
warning: section .data not found in /oradata/orabase/product/9.2.0/lib/libwtc9.sl
(no debugging symbols found)...(no debugging symbols found)...
#0 0xc00000000004d5f8 in _instantiate_iconv+0x58 () from /usr/lib/nls/iconv/pa20_64/methods.2/lhp15utf8.sl
(gdb) where
#0 0xc00000000004d5f8 in _instantiate_iconv+0x58 () from /usr/lib/nls/iconv/pa20_64/methods.2/lhp15utf8.sl
#1 0xc000000000315200 in iconv+0x30 () from /usr/lib/pa20_64/libc.2
#2 0x40000000000039f8 in Convert+0x100 ()
#3 0x4000000000003b5c in main+0x8c ()
(gdb) quit
training:/cosys06/cosys/edi/test>


So, how can i solve this problem?
Dennis Handly
Acclaimed Contributor

Re: iconv cause core dump

>+DA2.0W

Do not use this obsolete option. Replace by +DD64. Also add +M2 to help on 64 bit porting issues.

>how can i solve this problem?

Look at the warnings I mentioned. I also told you:
This is your problem. in_len and out_len MUST be size_t.
yuyuguang
Occasional Advisor

Re: iconv cause core dump

Hi Dennis Handly├п┬╝

It is fine when i change the code
int ilen, olen;---->size_t ilen, olen;


ilen = in_len;----->ilen = (size_t)in_len;
olen = out_len;---->olen = (size_t)out_len;

printf("2\n");
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);---->
ret = iconv(c_pt, &sin, &ilen, &sout, &olen);


But i casted to size_t in function iconv, why it core dump,
ret = iconv(c_pt, &sin, (size_t*)&in_len, &sout, (size_t*)&out_len);/*cast in iconv function*/

I don'tknow why it core dump in this way.
Dennis Handly
Acclaimed Contributor

Re: iconv cause core dump

>But I cast to size_t in function iconv, why it core dump?

Ah, a better question.

This type of cast in C++ is called an reinterpret cast. 5.2.10(7) says the results of the cast are unspecified. Your case also violates the alignment constraint since size_t is more strictly aligned than an int.

So your case is illegal because it violates the Standard. And for HP-UX it has these two problems:
1) The alignment of an int is 4 but size_t is 8. 50% of the time you could be getting an alignment trap.
2) iconv(3) thinks your data size is 8 bytes but you are passing in only 4. So your sizes are your values multiplied by 4 G plus a random value from 0 to 4 Gb.