Bugfix: crash when ":" in key string of protected fields

This commit is contained in:
Philipp Crocoll 2014-01-02 20:30:15 +01:00
parent a03aaaf5a5
commit f78116c6a1
4 changed files with 52 additions and 42 deletions

View File

@ -728,7 +728,7 @@ namespace KeePassLib
/// <param name="listStorage">Entry list in which the search results will /// <param name="listStorage">Entry list in which the search results will
/// be stored.</param> /// be stored.</param>
/// <param name="resultContexts">Dictionary that will be populated with text fragments indicating the context of why each entry (keyed by Uuid) was returned</param> /// <param name="resultContexts">Dictionary that will be populated with text fragments indicating the context of why each entry (keyed by Uuid) was returned</param>
public void SearchEntries(SearchParameters sp, PwObjectList<PwEntry> listStorage, IDictionary<PwUuid, String> resultContexts, public void SearchEntries(SearchParameters sp, PwObjectList<PwEntry> listStorage, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts,
IStatusLogger slStatus) IStatusLogger slStatus)
{ {
if(sp == null) { Debug.Assert(false); return; } if(sp == null) { Debug.Assert(false); return; }
@ -797,7 +797,7 @@ namespace KeePassLib
} }
private bool SearchEntriesSingle(SearchParameters spIn, private bool SearchEntriesSingle(SearchParameters spIn,
PwObjectList<PwEntry> listStorage, IDictionary<PwUuid, String> resultContexts, IStatusLogger slStatus, PwObjectList<PwEntry> listStorage, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts, IStatusLogger slStatus,
ref ulong uCurEntries, ulong uTotalEntries) ref ulong uCurEntries, ulong uTotalEntries)
{ {
SearchParameters sp = spIn.Clone(); SearchParameters sp = spIn.Clone();
@ -943,7 +943,7 @@ namespace KeePassLib
} }
private static void SearchEvalAdd(SearchParameters sp, string strDataField, private static void SearchEvalAdd(SearchParameters sp, string strDataField,
Regex rx, PwEntry pe, PwObjectList<PwEntry> lResults, IDictionary<PwUuid, String> resultContexts, string contextFieldName) Regex rx, PwEntry pe, PwObjectList<PwEntry> lResults, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts, string contextFieldName)
{ {
bool bMatch = false; bool bMatch = false;
int matchPos; int matchPos;
@ -993,7 +993,7 @@ namespace KeePassLib
var startPos = Math.Min(matchPos - (SearchContextStringMaxLength / 10), contextString.Length - SearchContextStringMaxLength); var startPos = Math.Min(matchPos - (SearchContextStringMaxLength / 10), contextString.Length - SearchContextStringMaxLength);
contextString = "… " + contextString.Substring(startPos, SearchContextStringMaxLength) + ((startPos + SearchContextStringMaxLength < contextString.Length) ? " …" : null); contextString = "… " + contextString.Substring(startPos, SearchContextStringMaxLength) + ((startPos + SearchContextStringMaxLength < contextString.Length) ? " …" : null);
} }
resultContexts[pe.Uuid] = contextFieldName + ": " + contextString; resultContexts[pe.Uuid] = new KeyValuePair<string, string>(contextFieldName, contextString);
} }
} }
} }

View File

@ -44,7 +44,7 @@ namespace keepass2android
return Search(database, sp, null); return Search(database, sp, null);
} }
public PwGroup Search(Database database, SearchParameters sp, IDictionary<PwUuid, String> resultContexts) public PwGroup Search(Database database, SearchParameters sp, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts)
{ {
if(sp.RegularExpression) // Validate regular expression if(sp.RegularExpression) // Validate regular expression

View File

@ -141,7 +141,7 @@ namespace keepass2android
} }
public PwGroup Search(SearchParameters searchParams, IDictionary<PwUuid, String> resultContexts) public PwGroup Search(SearchParameters searchParams, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts)
{ {
return SearchHelper.Search(this, searchParams, resultContexts); return SearchHelper.Search(this, searchParams, resultContexts);
} }

View File

@ -15,6 +15,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>. along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Globalization;
using System.Linq; using System.Linq;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
@ -80,7 +81,7 @@ namespace keepass2android.search
{ {
try try
{ {
var resultsContexts = new Dictionary<PwUuid, String>(); var resultsContexts = new Dictionary<PwUuid, KeyValuePair<string, string>>();
var result = _db.Search(new SearchParameters { SearchString = searchString }, resultsContexts ); var result = _db.Search(new SearchParameters { SearchString = searchString }, resultsContexts );
return new GroupCursor(result, resultsContexts); return new GroupCursor(result, resultsContexts);
} }
@ -172,9 +173,9 @@ namespace keepass2android.search
}; };
private readonly PwGroup mGroup; private readonly PwGroup mGroup;
private readonly IDictionary<PwUuid, String> mResultContexts; private readonly IDictionary<PwUuid, KeyValuePair<string, string>> mResultContexts;
public GroupCursor(PwGroup group, IDictionary<PwUuid, String> resultContexts) public GroupCursor(PwGroup group, IDictionary<PwUuid, KeyValuePair<string, string>> resultContexts)
{ {
System.Diagnostics.Debug.Assert(!group.Groups.Any(), "Expecting a flat list of groups"); System.Diagnostics.Debug.Assert(!group.Groups.Any(), "Expecting a flat list of groups");
@ -227,15 +228,14 @@ namespace keepass2android.search
switch (column) switch (column)
{ {
case 0: // _ID case 0: // _ID
return MPos.ToString(); return MPos.ToString(CultureInfo.InvariantCulture);
case 1: // SuggestColumnText1 case 1: // SuggestColumnText1
return CurrentEntry.Strings.ReadSafe(PwDefs.TitleField); return CurrentEntry.Strings.ReadSafe(PwDefs.TitleField);
case 2: // SuggestColumnText2 case 2: // SuggestColumnText2
string context; KeyValuePair<string, string> context;
if (mResultContexts.TryGetValue(CurrentEntry.Uuid, out context)) if (mResultContexts.TryGetValue(CurrentEntry.Uuid, out context))
{ {
context = Internationalise(context); return Internationalise(context);
return context;
} }
return null; return null;
case 3: // SuggestColumnIcon1 case 3: // SuggestColumnIcon1
@ -253,42 +253,52 @@ namespace keepass2android.search
} }
} }
private string Internationalise(string context) private string Internationalise(KeyValuePair<string, string> context)
{ {
// Some context names can be internationalised. try
var splitPos = context.IndexOf(':');
var rawName = context.Substring(0, splitPos);
int intlResourceId = 0;
switch (rawName)
{ {
case PwDefs.TitleField:
// We will already be showing Title, so ignore it entirely so it doesn't double-appear // Some context names can be internationalised.
return null; int intlResourceId = 0;
case PwDefs.UserNameField: switch (context.Key)
intlResourceId = Resource.String.entry_user_name; {
break; case PwDefs.TitleField:
case PwDefs.UrlField: // We will already be showing Title, so ignore it entirely so it doesn't double-appear
intlResourceId = Resource.String.entry_url;
break;
case PwDefs.NotesField:
intlResourceId = Resource.String.entry_comment;
break;
case PwGroup.SearchContextTags:
intlResourceId = Resource.String.entry_tags;
break;
default:
//don't disclose protected strings:
if (CurrentEntry.Strings.Get(rawName).IsProtected)
return null; return null;
break; case PwDefs.UserNameField:
} intlResourceId = Resource.String.entry_user_name;
break;
case PwDefs.UrlField:
intlResourceId = Resource.String.entry_url;
break;
case PwDefs.NotesField:
intlResourceId = Resource.String.entry_comment;
break;
case PwGroup.SearchContextTags:
intlResourceId = Resource.String.entry_tags;
break;
default:
//don't disclose protected strings:
if (CurrentEntry.Strings.Get(context.Key).IsProtected)
return null;
break;
}
if (intlResourceId > 0) if (intlResourceId > 0)
{
return Application.Context.GetString(intlResourceId) + ": "+context.Value;
}
return context.Key + ": " + context.Value;
}
catch (Exception)
{ {
return App.Context.GetString(intlResourceId) + context.Substring(splitPos); return null;
} }
return context;
} }
public override bool IsNull(int column) public override bool IsNull(int column)