fix crashes when filter strings are too long

This commit is contained in:
Eric House 2020-08-11 14:13:11 -07:00
parent b6a74b7d15
commit 7a43e95aa8
7 changed files with 259 additions and 188 deletions

View file

@ -700,10 +700,14 @@ public class DictBrowseDelegate extends DelegateBase
public void run() {
stopProgress();
if ( null != wrapper ) {
m_browseState.onFilterAccepted( m_dict, null );
initList( wrapper );
setFindPats( m_browseState.m_pats );
} else {
makeOkOnlyBuilder(R.string.alrt_bad_filter )
.show();
}
newFeatureAlert();
}
} );

View file

@ -619,9 +619,13 @@ public class XwJNI {
new Thread( new Runnable() {
@Override
public void run() {
IterWrapper wrapper = null;
long iterPtr = di_init( jniState, dictPtr, pats,
minLen, maxLen );
callback.onIterReady( new IterWrapper(iterPtr) );
if ( 0 != iterPtr ) {
wrapper = new IterWrapper(iterPtr);
}
callback.onIterReady( wrapper );
}
} ).start();
}

View file

@ -1615,6 +1615,8 @@
<!-- Shown when user tries to make a wordlist filter where max < min -->
<string name="error_min_gt_max">The minimum length value cannot be
greater than the maximum value.</string>
<!-- shown when something goes wrong building a filter -->
<string name="alrt_bad_filter">Unable to apply filter.</string>
<!-- Title of progress alert shown while wordlist is loading. For
huge lists like Polish this can take a few seconds. -->

View file

@ -2656,34 +2656,32 @@ Java_org_eehouse_android_xw4_jni_XwJNI_di_1init
DictionaryCtxt* dict = (DictionaryCtxt*)dictPtr;
if ( !!dict ) {
DictIterData* data = XP_CALLOC( globalState->mpool, sizeof(*data) );
data->globalState = globalState;
data->dict = dict_ref( dict, env );
data->depth = 2;
#ifdef DEBUG
data->guard = GI_GUARD;
#endif
PatDesc patDescs[3];
XP_MEMSET( patDescs, 0, VSIZE(patDescs) * sizeof(patDescs[0]) );
int len = 0;
bool formatOK = true;
if ( !!jPatsArr ) {
len = (*env)->GetArrayLength( env, jPatsArr );
XP_ASSERT( len == 3 );
for ( int ii = 0; ii < len ; ++ii ) {
for ( int ii = 0; formatOK && ii < len ; ++ii ) {
jobject jdesc = (*env)->GetObjectArrayElement( env, jPatsArr, ii );
if ( !!jdesc ) {
jbyteArray jtiles;
if ( getObject( env, jdesc, "tilePat", "[B", &jtiles ) ) {
int nTiles = (*env)->GetArrayLength( env, jtiles );
if ( 0 < nTiles ) {
patDescs[ii].nTiles = nTiles;
PatDesc* pd = &patDescs[ii];
/* If user adds too many tiles, we'll see it here */
if ( nTiles <= VSIZE(pd->tiles) ) {
pd->nTiles = nTiles;
jbyte* tiles = (*env)->GetByteArrayElements( env, jtiles, NULL );
XP_MEMCPY( &patDescs[ii].tiles[0], tiles,
nTiles * sizeof(patDescs[ii].tiles[0]) );
XP_MEMCPY( &pd->tiles[0], tiles, nTiles * sizeof(pd->tiles[0]) );
(*env)->ReleaseByteArrayElements( env, jtiles, tiles, 0 );
patDescs[ii].anyOrderOk = getBool( env, jdesc, "anyOrderOk" );
pd->anyOrderOk = getBool( env, jdesc, "anyOrderOk" );
} else {
formatOK = false;
}
}
deleteLocalRef( env, jtiles );
}
@ -2692,15 +2690,28 @@ Java_org_eehouse_android_xw4_jni_XwJNI_di_1init
}
}
DictIter* iter = NULL;
if ( formatOK ) {
DIMinMax mm = { .min = minLen, .max = maxLen };
data->iter = di_makeIter( data->dict, env, &mm, NULL, 0,
iter = di_makeIter( dict, env, &mm, NULL, 0,
!!jPatsArr ? patDescs : NULL, VSIZE(patDescs) );
}
if ( !!iter ) {
DictIterData* data = XP_CALLOC( globalState->mpool, sizeof(*data) );
data->iter = iter;
data->globalState = globalState;
data->dict = dict_ref( dict, env );
data->depth = 2;
#ifdef DEBUG
data->guard = GI_GUARD;
#endif
makeIndex( data );
(void)di_firstWord( data->iter );
closure = (jlong)data;
}
}
return closure;
}

View file

@ -145,6 +145,27 @@ typedef enum { PatErrNone,
PatErrDupInSet,
} PatErr;
#ifdef DEBUG
static const XP_UCHAR*
patErrToStr( PatErr err )
{
const XP_UCHAR* result = NULL;
# define CASESTR(s) case s: result = #s; break
switch ( err ) {
CASESTR(PatErrNone);
CASESTR(PatErrMissingClose);
CASESTR(PatErrMultipleSpellings);
CASESTR(PatErrBadCountTerm);
CASESTR(PatErrNoDigit);
CASESTR(PatErrTooComplex);
CASESTR(PatErrBogusTiles);
CASESTR(PatErrDupInSet);
}
# undef CASESTR
return result;
}
#endif
typedef struct _ParseState {
const DictionaryCtxt* dict;
const XP_UCHAR* pat;
@ -264,6 +285,18 @@ onFoundTiles( void* closure, const Tile* tiles, int len )
return 1 == len && PatErrNone == data->err;
}
static PatErr
addElem( ParseState* ps, PatElem* elem )
{
PatErr err = PatErrNone;
if ( ps->elemIndex < VSIZE(ps->elems) ) {
ps->elems[ps->elemIndex++] = *elem;
} else {
err = PatErrTooComplex;
}
return err;
}
static PatErr
parseTile( ParseState* ps )
{
@ -390,9 +423,10 @@ compileParent( ParseState* ps )
return err;
}
static void
static PatErr
initPS( ParseState* ps, const DictionaryCtxt* dict )
{
PatErr result = PatErrNone;
XP_MEMSET( ps, 0, sizeof(*ps) );
XP_ASSERT( !!dict );
ps->dict = dict;
@ -407,8 +441,9 @@ initPS( ParseState* ps, const DictionaryCtxt* dict )
#ifdef WITH_START
PatElem start = { .typ = START, };
ps->elems[ps->elemIndex++] = start;
result = addElem( ps, &start );
#endif
return result;
}
static XP_Bool
@ -416,19 +451,13 @@ compilePat( ParseState* ps, const XP_UCHAR* strPat )
{
ps->pat = strPat;
ps->patIndex = 0;
// XP_ASSERT( !!iter->dict );
/* ParseState ps = { .dict = iter->dict, */
/* .pat = strPat, */
/* .blankMask = ((TileSet)1) << dict_getBlankTile( iter->dict ), */
/* }; */
PatErr err = compileParent( ps );
XP_Bool success = err == PatErrNone && 0 < ps->elemIndex;
/* if ( success ) { */
/* XP_ASSERT( ps.elemIndex < VSIZE(ps.elems) ); */
/* replacePat( iter, &ps ); */
/* } */
if ( !success ) {
XP_LOGFF( "=> %s", patErrToStr(err) );
}
return success;
}
@ -1309,22 +1338,25 @@ firstWord( DictIter* iter, XP_Bool log )
return success;
}
static void
static XP_Bool
addTilePats( ParseState* ps, const PatDesc* pd )
{
XP_Bool success = XP_TRUE;
XP_Bool anyOrderOk = pd->anyOrderOk;
PatElem elem = { .typ = CHILD,
.minMatched = 1,
.maxMatched = 1,
};
for ( int ii = 0; ii < pd->nTiles; ++ii ) {
for ( int ii = 0; success && ii < pd->nTiles; ++ii ) {
#ifdef MULTI_SET
++elem.u.child.tiles.cnts[pd->tiles[ii]];
#else
elem.u.child.tiles |= 1 << pd->tiles[ii];
#endif
if ( !anyOrderOk ) {
ps->elems[ps->elemIndex++] = elem;
success = ps->elemIndex < VSIZE(ps->elems);
if ( success ) {
success = PatErrNone == addElem( ps, &elem );
#ifdef MULTI_SET
XP_MEMSET( &elem.u.child.tiles, 0, sizeof(elem.u.child.tiles) );
#else
@ -1332,11 +1364,14 @@ addTilePats( ParseState* ps, const PatDesc* pd )
#endif
}
}
}
if ( anyOrderOk ) {
elem.u.child.flags |= FLAG_SINGLE;
elem.minMatched = elem.maxMatched = pd->nTiles;
ps->elems[ps->elemIndex++] = elem;
success = PatErrNone == addElem( ps, &elem );
}
LOG_RETURNF( "%s", boolToStr(success) );
return success;
}
static void
@ -1351,7 +1386,12 @@ addWildcard( ParseState* ps )
#else
elem.u.child.tiles = ps->blankMask;
#endif
ps->elems[ps->elemIndex++] = elem;
#ifdef DEBUG
PatErr err =
#endif
addElem( ps, &elem );
XP_ASSERT( err == PatErrNone );
}
static void
@ -1404,7 +1444,8 @@ di_makeIter( const DictionaryCtxt* dict, XWEnv xwe, const DIMinMax* minmax,
if ( ii != STARTS_WITH ) {
addWildcard( &ps );
}
addTilePats( &ps, ta );
success = addTilePats( &ps, ta );
if ( success ) {
if ( ii != ENDS_WITH ) {
addWildcard( &ps );
}
@ -1412,6 +1453,7 @@ di_makeIter( const DictionaryCtxt* dict, XWEnv xwe, const DIMinMax* minmax,
}
}
}
}
if ( success ) {
XP_LOGFF( "making iter of size %zu", sizeof(*iter) );
iter = XP_CALLOC( dict->mpool, sizeof(*iter) );

View file

@ -77,7 +77,7 @@ typedef struct DictIter DictIter;
* ends-with. The first is more powerful but I'm not sure it'll ever be part
* of a shipping UI.*/
typedef struct _PatDesc {
Tile tiles[16];
Tile tiles[MAX_COLS_DICT];
XP_U16 nTiles;
XP_Bool anyOrderOk;
} PatDesc;

View file

@ -1948,7 +1948,6 @@ tmp_noop_sigintterm( int XP_UNUSED(sig) )
exit(0);
}
typedef struct _FTD {
PatDesc* desc;
XP_Bool called;
@ -1960,9 +1959,7 @@ onFoundTiles2( void* closure, const Tile* tiles, int nTiles )
FTD* data = (FTD*)closure;
if ( data->called ) {
XP_LOGFF( "ERROR: called more than once; Hungarian case???" );
} else if ( nTiles > VSIZE(data->desc->tiles) ) {
XP_ASSERT(0);
} else {
} else if ( nTiles <= VSIZE(data->desc->tiles) ) {
data->called = XP_TRUE;
data->desc->nTiles = nTiles;
XP_MEMCPY( &data->desc->tiles[0], tiles, nTiles * sizeof(tiles[0]) );
@ -2014,7 +2011,10 @@ patsParamsToIter( const LaunchParams* params, const DictionaryCtxt* dict )
}
DictIter* iter = di_makeIter( dict, NULL_XWE, dimmp, strPats, nStrPats,
descs, nPatDescs );
nPatDescs == 0 ? NULL : descs, nPatDescs );
if ( !iter ) {
XP_LOGFF( "Unable to build iter" );
}
return iter;
}
@ -2025,6 +2025,7 @@ testGetNthWord( const LaunchParams* params, const DictionaryCtxt* dict,
const IndexData* data )
{
DictIter* iter = patsParamsToIter( params, dict );
if ( !!iter ) {
XP_U32 half = di_countWords( iter, NULL ) / 2;
XP_U32 interval = half / 100;
const XP_UCHAR* delim = params->dumpDelim; /* NULL is ok */
@ -2056,6 +2057,7 @@ testGetNthWord( const LaunchParams* params, const DictionaryCtxt* dict,
}
}
di_freeIter( iter, NULL_XWE );
}
}
typedef struct _FTData {
@ -2109,6 +2111,7 @@ walk_dict_test( MPFORMAL const LaunchParams* params, const DictionaryCtxt* dict,
{
DictIter* iter = patsParamsToIter( params, dict );
if ( !!iter ) {
LengthsArray lens;
XP_U32 count = di_countWords( iter, &lens );
@ -2226,7 +2229,8 @@ walk_dict_test( MPFORMAL const LaunchParams* params, const DictionaryCtxt* dict,
XP_FREE( mpool, data.prefixes );
}
di_freeIter( iter, NULL_XWE );
XP_LOGF( "done" );
}
XP_LOGFF( "done" );
}
static void
@ -2253,6 +2257,7 @@ static void
dumpDict( const LaunchParams* params, DictionaryCtxt* dict )
{
DictIter* iter = patsParamsToIter( params, dict );
if ( !!iter ) {
const XP_UCHAR* delim = params->dumpDelim; /* NULL is ok */
for ( XP_Bool result = di_firstWord( iter );
result;
@ -2262,6 +2267,7 @@ dumpDict( const LaunchParams* params, DictionaryCtxt* dict )
fprintf( stdout, "%s\n", buf );
}
di_freeIter( iter, NULL_XWE );
}
}
static void
@ -2662,12 +2668,14 @@ testOneString( const LaunchParams* params, GSList* testDicts )
params->useMmap );
if ( NULL != dict ) {
DictIter* iter = patsParamsToIter( params, dict );
if ( !!iter ) {
if ( ! di_stringMatches( iter, params->iterTestPatStr ) ) {
result = 1;
}
di_freeIter( iter, NULL_XWE );
}
}
}
return result;
}
#endif