From 12d8a092d712a73381b795d6ab061eff2a865891 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 12 Oct 2022 11:27:28 -0700 Subject: [PATCH] move getUsername() to dutils to avoid crash Being in util meant the default implementation got called, and that returned a null string: boom. --- .../eehouse/android/xw4/jni/DUtilCtxt.java | 11 +++++ xwords4/android/jni/utilwrapper.c | 40 ++++++++++++------- xwords4/common/dutil.h | 9 ++++- xwords4/common/game.c | 5 ++- xwords4/common/util.h | 7 ---- xwords4/linux/lindutil.c | 10 +++++ xwords4/linux/linuxmain.c | 11 ----- 7 files changed, 57 insertions(+), 36 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java index d99dd2938..d99d82947 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java @@ -243,6 +243,17 @@ public class DUtilCtxt { public static final int PAUSED = 1; public static final int AUTOPAUSED = 2; + // PENDING use prefs for this + public String getUsername( int posn, boolean isLocal, boolean isRobot ) + { + Log.d( TAG, "getUsername(posn=%d; isLocal=%b, isRobot=%b)", + posn, isLocal, isRobot ); + String fmt = isLocal ? "Lcl" : "Rmt"; + fmt += isRobot ? "Rbt" : "Hum"; + fmt += " %d"; + return String.format( fmt, posn + 1 ); + } + // A pause can come in when a game's open or when it's not. If it's open, // we want to post an alert. If it's not, we want to post a notification, // or at least kick off DupeModeTimer to cancel or start the timer-running diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index 5202e920e..6d4356061 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -303,20 +303,6 @@ and_util_getDict( XW_UtilCtxt* uc, XWEnv xwe, return dict; } -static void -and_util_getUsername( XW_UtilCtxt* uc, XWEnv xwe, XP_Bool isLocal, - XP_Bool isRobot, XP_U16 num, - XP_UCHAR* buf, XP_U16* len ) -{ - /* PENDING: this needs to go through to java */ - XP_USE( uc ); - XP_USE( xwe ); - XP_USE( isRobot ); - XP_USE( isLocal ); - *len = XP_SNPRINTF( buf, *len-1, "Plyr #%d", num + 1 ); - XP_ASSERT( '\0' == buf[*len] ); -} - static void and_util_notifyGameOver( XW_UtilCtxt* uc, XWEnv xwe, XP_S16 XP_UNUSED(quitter) ) { @@ -866,6 +852,30 @@ and_dutil_md5sum( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* ptr, XP_U32 len ) } #endif +static void +and_dutil_getUsername( XW_DUtilCtxt* duc, XWEnv xwe, XP_U16 num, + XP_Bool isLocal, XP_Bool isRobot, + XP_UCHAR* buf, XP_U16* lenp ) +{ + LOG_FUNC(); + DUTIL_CBK_HEADER( "getUsername", "(IZZ)Ljava/lang/String;" ); + + jstring jresult = (*env)->CallObjectMethod( env, dutil->jdutil, mid, + num, isLocal, isRobot ); + jsize len = (*env)->GetStringUTFLength( env, jresult ); + if ( len < *lenp ) { + const char* jchars = (*env)->GetStringUTFChars( env, jresult, NULL ); + XP_LOGFF( "len(\"%s\"): %d", jchars, len ); + *lenp = XP_SNPRINTF( buf, len+1, "%s", jchars ); + XP_LOGFF( "snprintf wrote: '%s'", buf ); + // *lenp = len; + (*env)->ReleaseStringUTFChars( env, jresult, jchars ); + deleteLocalRef( env, jresult ); + } + DUTIL_CBK_TAIL(); + LOG_RETURNF( "%s", buf ); +} + static void and_dutil_notifyPause( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, DupPauseType pauseTyp, XP_U16 pauser, const XP_UCHAR* name, @@ -1009,7 +1019,6 @@ makeUtil( MPFORMAL JNIEnv* env, SET_PROC(informUndo); SET_PROC(informNetDict); SET_PROC(getDict); - SET_PROC(getUsername); SET_PROC(notifyGameOver); #ifdef XWFEATURE_HILITECELL SET_PROC(hiliteCell); @@ -1117,6 +1126,7 @@ makeDUtil( MPFORMAL JNIEnv* env, #ifdef COMMS_CHECKSUM SET_DPROC(md5sum); #endif + SET_DPROC(getUsername); SET_DPROC(notifyPause); SET_DPROC(haveGame); SET_DPROC(onDupTimerChanged); diff --git a/xwords4/common/dutil.h b/xwords4/common/dutil.h index f7e51d82a..8704edaad 100644 --- a/xwords4/common/dutil.h +++ b/xwords4/common/dutil.h @@ -1,6 +1,6 @@ /* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */ /* - * Copyright 2018 by Eric House (xwords@eehouse.org). All rights + * Copyright 2018 - 2022 by Eric House (xwords@eehouse.org). All rights * reserved. * * This program is free software; you can redistribute it and/or @@ -79,6 +79,9 @@ typedef struct _DUtilVtable { XP_UCHAR* (*m_dutil_md5sum)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* ptr, XP_U32 len ); #endif + void (*m_dutil_getUsername)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U16 num, + XP_Bool isLocal, XP_Bool isRobot, + XP_UCHAR* buf, XP_U16* len ); void (*m_dutil_notifyPause)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, DupPauseType pauseTyp, XP_U16 pauser, const XP_UCHAR* name, const XP_UCHAR* msg ); @@ -151,6 +154,10 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil ); (duc)->vtable.m_dutil_md5sum((duc), (e), (p), (l)) #endif +#define dutil_getUsername(duc, xwe, num, isLocal, isRobot, buf, lenp) \ + (duc)->vtable.m_dutil_getUsername((duc), (xwe), (num), (isLocal), \ + (isRobot), (buf), (lenp)) + #define dutil_notifyPause( duc, e, id, ip, p, n, m ) \ (duc)->vtable.m_dutil_notifyPause( (duc), (e), (id), (ip), (p), (n), (m) ) diff --git a/xwords4/common/game.c b/xwords4/common/game.c index b9bd2bf00..f16f80f52 100644 --- a/xwords4/common/game.c +++ b/xwords4/common/game.c @@ -871,8 +871,9 @@ gi_setNPlayers( CurGameInfo* gi, XWEnv xwe, XW_UtilCtxt* util, if ( !lp->name || !lp->name[0] ) { XP_UCHAR name[32]; XP_U16 len = VSIZE(name); - util_getUsername( util, xwe, LP_IS_LOCAL(lp), - LP_IS_ROBOT(lp), ii, name, &len ); + dutil_getUsername( util_getDevUtilCtxt( util, xwe ), + xwe, ii, LP_IS_LOCAL(lp), + LP_IS_ROBOT(lp), name, &len ); replaceStringIfDifferent( util->mpool, &lp->name, name ); } } diff --git a/xwords4/common/util.h b/xwords4/common/util.h index fa4c22a3b..0b4436666 100644 --- a/xwords4/common/util.h +++ b/xwords4/common/util.h @@ -126,9 +126,6 @@ typedef struct UtilVtable { const DictionaryCtxt* (*m_util_getDict)( XW_UtilCtxt* uc, XWEnv xwe, const XP_UCHAR* isoCode, const XP_UCHAR* dictName ); - void (*m_util_getUsername)( XW_UtilCtxt* uc, XWEnv xwe, XP_Bool isLocal, - XP_Bool isRobot, XP_U16 num, - XP_UCHAR* buf, XP_U16* len ); void (*m_util_notifyGameOver)( XW_UtilCtxt* uc, XWEnv xwe, XP_S16 quitter ); #ifdef XWFEATURE_HILITECELL XP_Bool (*m_util_hiliteCell)( XW_UtilCtxt* uc, XWEnv xwe, XP_U16 col, XP_U16 row ); @@ -262,10 +259,6 @@ struct XW_UtilCtxt { #define util_getDict( uc, xwe, isoCode, dictName ) \ (uc)->vtable->m_util_getDict((uc), (xwe), (isoCode), (dictName)) -#define util_getUsername(uc, xwe, isLocal, isRobot, num, buf, lenp) \ - (uc)->vtable->m_util_getUsername((uc), (xwe), (isLocal), (isRobot), (num), \ - (buf), (lenp)) - #define util_notifyGameOver( uc,e, q ) \ (uc)->vtable->m_util_notifyGameOver((uc), (e), (q)) diff --git a/xwords4/linux/lindutil.c b/xwords4/linux/lindutil.c index 33ceac136..b4689edbb 100644 --- a/xwords4/linux/lindutil.c +++ b/xwords4/linux/lindutil.c @@ -63,6 +63,15 @@ static XP_UCHAR* linux_dutil_md5sum( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* XP_U32 len ); #endif +static void +linux_dutil_getUsername( XW_DUtilCtxt* XP_UNUSED(duc), XWEnv XP_UNUSED(xwe), + XP_U16 num, XP_Bool XP_UNUSED(isLocal), XP_Bool isRobot, + XP_UCHAR* buf, XP_U16* len ) +{ + const char* fmt = isRobot ? "Robot %d" : "Player %d"; + *len = XP_SNPRINTF( buf, *len, fmt, num ); +} + static void linux_dutil_notifyPause( XW_DUtilCtxt* XP_UNUSED(duc), XWEnv XP_UNUSED(xwe), XP_U32 XP_UNUSED_DBG(gameID), @@ -185,6 +194,7 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) #ifdef COMMS_CHECKSUM SET_PROC(md5sum); #endif + SET_PROC(getUsername); SET_PROC(notifyPause); SET_PROC(haveGame); SET_PROC(onDupTimerChanged); diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index 4ccdb3c98..b143ee708 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -2414,16 +2414,6 @@ linux_util_requestTime( XW_UtilCtxt* uc, XWEnv XP_UNUSED(xwe) ) cGlobals->idleID = g_idle_add( idle_func, cGlobals ); } /* gtk_util_requestTime */ - -static void -linux_util_getUsername( XW_UtilCtxt* XP_UNUSED(uc), XWEnv XP_UNUSED(xwe), - XP_Bool XP_UNUSED(isLocal), XP_Bool isRobot, XP_U16 num, - XP_UCHAR* buf, XP_U16* len ) -{ - const char* fmt = isRobot ? "Robot %d" : "Player %d"; - *len = XP_SNPRINTF( buf, *len, fmt, num ); -} - void setupLinuxUtilCallbacks( XW_UtilCtxt* util ) { @@ -2436,7 +2426,6 @@ setupLinuxUtilCallbacks( XW_UtilCtxt* util ) SET_PROC(setTimer); SET_PROC(clearTimer); SET_PROC(requestTime); - SET_PROC(getUsername); #undef SET_PROC }