From aba78f3baad11db8e626d1a4554cb157a9547ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Wed, 10 Jul 2019 23:08:58 +0200 Subject: [PATCH] Properly handle self-asignments --- src/map/matrix.cpp | 26 ++++++++++++++------------ src/map/projection.cpp | 14 ++++++++------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/map/matrix.cpp b/src/map/matrix.cpp index ca5d0209..054ee5ef 100644 --- a/src/map/matrix.cpp +++ b/src/map/matrix.cpp @@ -37,21 +37,23 @@ Matrix::Matrix(const Matrix& M) Matrix &Matrix::operator=(const Matrix &M) { - if (_h != M._h || _w != M._w) { - if (!isNull()) - delete[] _m; + if (this != &M) { + if (_h != M._h || _w != M._w) { + if (!isNull()) + delete[] _m; - _h = M._h; _w = M._w; - if (isNull()) - _m = 0; - else - _m = new double[_h * _w]; + _h = M._h; _w = M._w; + if (isNull()) + _m = 0; + else + _m = new double[_h * _w]; + } + + for (size_t i = 0; i < _h; i++) + for (size_t j = 0; j < _w; j++) + m(i,j) = M.m(i,j); } - for (size_t i = 0; i < _h; i++) - for (size_t j = 0; j < _w; j++) - m(i,j) = M.m(i,j); - return *this; } diff --git a/src/map/projection.cpp b/src/map/projection.cpp index 7afb4a3b..1293c1ef 100644 --- a/src/map/projection.cpp +++ b/src/map/projection.cpp @@ -128,13 +128,15 @@ Projection::~Projection() Projection &Projection::operator=(const Projection &p) { - delete _ct; + if (this != &p) { + delete _ct; - _gcs = p._gcs; - _units = p._units; - _ct = p._ct ? p._ct->clone() : 0; - _geographic = p._geographic; - _cs = p._cs; + _gcs = p._gcs; + _units = p._units; + _ct = p._ct ? p._ct->clone() : 0; + _geographic = p._geographic; + _cs = p._cs; + } return *this; }