office-gobmx/vcl/source/bitmap/BitmapInfoAccess.cxx
Caolán McNamara 7d4142269e crashtesting: threaded scaling crash on re-export of ooo24840-1.sxw to odt
#13 0x00007f1cb843752a in o3tl::cow_wrapper<ImplBitmapPalette, o3tl::UnsafeRefCountingPolicy>::operator->() (this=0x5596086d5968) at include/o3tl/cow_wrapper.hxx:329
         __PRETTY_FUNCTION__ = "BitmapColor& BitmapPalette::operator[](sal_uInt16)"
 #14 0x00007f1cb843752a in BitmapPalette::operator[](unsigned short) (this=0x5596086d5968, nIndex=nIndex@entry=0) at vcl/source/bitmap/bitmappalette.cxx:139
         __PRETTY_FUNCTION__ = "BitmapColor& BitmapPalette::operator[](sal_uInt16)"
 #15 0x00007f1cb849f5f5 in BitmapInfoAccess::GetPaletteColor(unsigned short) const (nColor=0, this=0x5596085989f0) at include/vcl/BitmapInfoAccess.hxx:114
         __PRETTY_FUNCTION__ = "const BitmapColor& BitmapInfoAccess::GetPaletteColor(sal_uInt16) const"

the mpBuffer member of BitmapInfoAccess is

BitmapBuffer* mpBuffer;

not

const BitmapBuffer* mpBuffer;

so mpBuffer->maPalette.foo() calls non-const variants of foo(),
(BitmapPalette::operator[](unsigned short) in this case), which
is presumably non the expected outcome, as the copy-on-write mpImpl of
BitmapPalette unsafely creates a new copy its internals on the first
dereference of mpImpl in a non-const method.

Change-Id: I1ebb3c67386a9028e5b8bab4b2d1cc5862700aa1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121910
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
2021-09-10 19:01:33 +02:00

80 lines
2.4 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <vcl/BitmapInfoAccess.hxx>
#include <salbmp.hxx>
#include <svdata.hxx>
#include <salinst.hxx>
BitmapInfoAccess::BitmapInfoAccess(Bitmap& rBitmap, BitmapAccessMode nMode)
: mpBuffer(nullptr)
, mnAccessMode(nMode)
{
std::shared_ptr<SalBitmap> xImpBmp = rBitmap.ImplGetSalBitmap();
if (!xImpBmp)
return;
if (mnAccessMode == BitmapAccessMode::Write)
{
xImpBmp->DropScaledCache();
if (xImpBmp.use_count() > 2)
{
xImpBmp.reset();
rBitmap.ImplMakeUnique();
xImpBmp = rBitmap.ImplGetSalBitmap();
}
}
mpBuffer = xImpBmp->AcquireBuffer(mnAccessMode);
if (!mpBuffer)
{
std::shared_ptr<SalBitmap> xNewImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
if (xNewImpBmp->Create(*xImpBmp, rBitmap.getPixelFormat()))
{
xImpBmp = xNewImpBmp;
rBitmap.ImplSetSalBitmap(xImpBmp);
mpBuffer = xImpBmp->AcquireBuffer(mnAccessMode);
}
}
maBitmap = rBitmap;
}
BitmapInfoAccess::~BitmapInfoAccess()
{
std::shared_ptr<SalBitmap> xImpBmp = maBitmap.ImplGetSalBitmap();
if (mpBuffer && xImpBmp)
{
xImpBmp->ReleaseBuffer(mpBuffer, mnAccessMode);
}
}
sal_uInt16 BitmapInfoAccess::GetBestPaletteIndex(const BitmapColor& rBitmapColor) const
{
const BitmapBuffer* pBuffer = mpBuffer;
return (HasPalette() ? pBuffer->maPalette.GetBestIndex(rBitmapColor) : 0);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */