@@ -111,25 +111,78 @@ public List<User> getAllUsers() {
111111
112112 @ Override
113113 public boolean exists (Account account ) {
114- Account [] nextcloudAccounts = getAccounts ();
114+ try {
115+ if (account == null ) {
116+ Log_OC .d (TAG , "account is null" );
117+ return false ;
118+ }
119+
120+ Account [] nextcloudAccounts = getAccounts ();
121+ if (nextcloudAccounts .length == 0 ) {
122+ Log_OC .d (TAG , "nextcloudAccounts are empty" );
123+ return false ;
124+ }
125+
126+ if (account .name .isEmpty ()) {
127+ Log_OC .d (TAG , "account name is empty" );
128+ return false ;
129+ }
115130
116- if (account != null && account .name != null ) {
117131 int lastAtPos = account .name .lastIndexOf ('@' );
132+ if (lastAtPos == -1 ) {
133+ Log_OC .d (TAG , "lastAtPos cannot be found" );
134+ return false ;
135+ }
136+
137+ boolean isLastAtPosInBoundsForHostAndPort = lastAtPos + 1 < account .name .length ();
138+ if (!isLastAtPosInBoundsForHostAndPort ) {
139+ Log_OC .d (TAG , "lastAtPos not in bounds" );
140+ return false ;
141+ }
142+
118143 String hostAndPort = account .name .substring (lastAtPos + 1 );
144+
119145 String username = account .name .substring (0 , lastAtPos );
146+ if (hostAndPort .isEmpty () || username .isEmpty ()) {
147+ Log_OC .d (TAG , "hostAndPort or username is empty" );
148+ return false ;
149+ }
150+
120151 String otherHostAndPort ;
121152 String otherUsername ;
153+
122154 for (Account otherAccount : nextcloudAccounts ) {
155+ // Skip null accounts or accounts with null names
156+ if (otherAccount == null || otherAccount .name .isEmpty ()) {
157+ continue ;
158+ }
159+
123160 lastAtPos = otherAccount .name .lastIndexOf ('@' );
161+
162+ // Skip invalid account names
163+ if (lastAtPos == -1 ) {
164+ continue ;
165+ }
166+
167+ boolean isLastAtPosInBoundsForOtherHostAndPort = lastAtPos + 1 < otherAccount .name .length ();
168+ if (!isLastAtPosInBoundsForOtherHostAndPort ) {
169+ continue ;
170+ }
124171 otherHostAndPort = otherAccount .name .substring (lastAtPos + 1 );
172+
125173 otherUsername = otherAccount .name .substring (0 , lastAtPos );
174+
126175 if (otherHostAndPort .equals (hostAndPort ) &&
127176 otherUsername .equalsIgnoreCase (username )) {
128177 return true ;
129178 }
130179 }
180+
181+ return false ;
182+ } catch (Exception e ) {
183+ Log_OC .d (TAG , "Exception caught at UserAccountManagerImpl.exists(): " + e );
184+ return false ;
131185 }
132- return false ;
133186 }
134187
135188 @ Override
0 commit comments