Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / source / core / tool / lookupcache.cxx
blob70e19ec20d86e2b8842abd07050ee33f92392b35
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <lookupcache.hxx>
21 #include <document.hxx>
22 #include <queryentry.hxx>
23 #include <brdcst.hxx>
25 #include <sal/log.hxx>
27 ScLookupCache::QueryCriteria::QueryCriteria( const ScQueryEntry& rEntry ) :
28 mfVal(0.0), mbAlloc(false), mbString(false)
30 switch (rEntry.eOp)
32 case SC_EQUAL :
33 meOp = EQUAL;
34 break;
35 case SC_LESS_EQUAL :
36 meOp = LESS_EQUAL;
37 break;
38 case SC_GREATER_EQUAL :
39 meOp = GREATER_EQUAL;
40 break;
41 default:
42 meOp = UNKNOWN;
43 SAL_WARN( "sc.core", "ScLookupCache::QueryCriteria not prepared for this ScQueryOp");
46 const ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
47 if (rItem.meType == ScQueryEntry::ByString)
48 setString(rItem.maString.getString());
49 else
50 setDouble(rItem.mfVal);
53 ScLookupCache::QueryCriteria::QueryCriteria( const ScLookupCache::QueryCriteria & r ) :
54 mfVal( r.mfVal),
55 mbAlloc( false),
56 mbString( false),
57 meOp( r.meOp)
59 if (r.mbString && r.mpStr)
61 mpStr = new OUString( *r.mpStr);
62 mbAlloc = mbString = true;
66 ScLookupCache::QueryCriteria::~QueryCriteria()
68 deleteString();
71 ScLookupCache::Result ScLookupCache::lookup( ScAddress & o_rResultAddress,
72 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
74 auto it( maQueryMap.find( QueryKey( rQueryAddress,
75 rCriteria.getQueryOp())));
76 if (it == maQueryMap.end())
77 return NOT_CACHED;
78 const QueryCriteriaAndResult& rResult = (*it).second;
79 if (!(rResult.maCriteria == rCriteria))
80 return CRITERIA_DIFFERENT;
81 if (rResult.maAddress.Row() < 0 )
82 return NOT_AVAILABLE;
83 o_rResultAddress = rResult.maAddress;
84 return FOUND;
87 SCROW ScLookupCache::lookup( const QueryCriteria & rCriteria ) const
89 // try to find the row index for which we have already performed lookup
90 auto it = std::find_if(maQueryMap.begin(), maQueryMap.end(),
91 [&rCriteria](const std::pair<QueryKey, QueryCriteriaAndResult>& rEntry) {
92 return rEntry.second.maCriteria == rCriteria;
93 });
94 if (it != maQueryMap.end())
95 return it->first.mnRow;
97 // not found
98 return -1;
101 bool ScLookupCache::insert( const ScAddress & rResultAddress,
102 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
103 const bool bAvailable )
105 QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
106 QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
107 if (!bAvailable)
108 aResult.maAddress.SetRow(-1);
109 bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
110 QueryCriteriaAndResult>( aKey, aResult)).second;
112 return bInserted;
115 void ScLookupCache::Notify( const SfxHint& rHint )
117 if (!mpDoc->IsInDtorClear())
119 if (rHint.GetId() == SfxHintId::ScDataChanged || rHint.GetId() == SfxHintId::ScAreaChanged)
121 mpDoc->RemoveLookupCache( *this);
122 // this ScLookupCache is deleted by RemoveLookupCache
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */