When converting a string using libiconv, for certain combinations of strings and encodings, sometimes iconv() crashes
Assertion failed: (tmpin - *in <= *inbytes), function _citrus_iconv_std_iconv_convert, file citrus_iconv_std.c, line 1916.
This means that libiconv is a debug build since it asserts and also that it doesn't handles certain characters,
it should not crash but return some error code.
I've reported this problem twice to Apple, nothing happens.
FB17715360 and FB17567155
Below is a simple code snippet that illustrates the problem:
#include <iconv.h>
#import <Foundation/Foundation.h>
void TestLibIconv()
{
NSLog(@"Convert a string from some encoding to some other");
NSLog(@"The in string may have some unknown encoding");
const char *inEnc = "SHIFT_JIS"; // Japanese
//const char *inEnc = "CP1252"; // works
const char *outEnc = "UTF-8";
NSLog(@"inEnc %s outEnc %s", inEnc, outEnc);
iconv_t cd = iconv_open(outEnc, inEnc);
if (cd != (iconv_t) -1)
{
#if 1 // works when this flag isn't set
int on = 1; // allow illegal byte sequence
iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &on);
#endif
const size_t m_kMaxOutChars = 200;
char outBuffer[m_kMaxOutChars+1];
memset(outBuffer, 0, sizeof(outBuffer));
char inStr[] = "\"\xe5\""; // crashes - the '\"' character seems to be the problem
size_t inbytesleft = strlen(inStr);
size_t outbytesleft = m_kMaxOutChars;
char *pInBuf = inStr;
char *pOutBuf = outBuffer;
size_t retVal = iconv(cd, &pInBuf, &inbytesleft, &pOutBuf, &outbytesleft);
NSLog(@"iconv() returns %ld", retVal);
NSLog(@"outBuffer %s", outBuffer);
iconv_close(cd);
}
}