Repository: rasterio/rasterio

Issue:
Python crashes while building overviews
After performing the below code Python crashes:

```python
import rasterio
from rasterio.enums import Resampling

factors = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
dst = rasterio.open('rasterio/tests/data/RGB.byte.tif', 'r+')
dst.build_overviews(factors, Resampling.average)
```

```
*** Error in `python': malloc(): memory corruption: 0x0000000002e0f9c0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fbe1c3fd7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8213e)[0x7fbe1c40813e]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7fbe1c40a184]
/home/rykov/sandbox/env/lib/python3.5/site-packages/rasterio/.libs/libgdal-acedaae2.so.20.3.1(CPLMalloc+0x20)[0x7fbe19ab2700]
/home/rykov/sandbox/env/lib/python3.5/site-packages/rasterio/.libs/libgdal-acedaae2.so.20.3.1(CPLCalloc+0x1c)[0x7fbe19ab27ac]
/home/rykov/sandbox/env/lib/python3.5/site-packages/rasterio/.libs/libgdal-acedaae2.so.20.3.1(_ZN12GTiffDataset15IBuildOverviewsEPKciPiiS2_PFidS1_PvES3_+0x10f0)[0x7fbe19554bd0]
/home/rykov/sandbox/env/lib/python3.5/site-packages/rasterio/.libs/libgdal-acedaae2.so.20.3.1(_ZN11GDALDataset14BuildOverviewsEPKciPiiS2_PFidS1_PvES3_+0x38)[0x7fbe198059f8]
/home/rykov/sandbox/env/lib/python3.5/site-packages/rasterio/_io.cpython-35m-x86_64-linux-gnu.so(+0x3613a)[0x7fbe0595713a]
python(PyCFunction_Call+0x77)[0x4e9ba7]
python(PyEval_EvalFrameEx+0x614)[0x5372f4]
python[0x540199]
python(PyEval_EvalCode+0x1f)[0x540e4f]
python[0x60c272]
python(PyRun_InteractiveOneObject+0x2b1)[0x46b89f]
python(PyRun_InteractiveLoopFlags+0xe8)[0x46ba48]
python[0x46cfa0]
python[0x4cf2bd]
python(main+0xe1)[0x4cfeb1]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fbe1c3a6830]
python(_start+0x29)[0x5d6049]
```


Discussion:
Confirmed @drnextgis . One can't effectively create a 1/2048 overview of a dataset that's only about 700x800 pixels, but we should do better than crash.
Original issue was fixed in GDAL via https://github.com/OSGeo/gdal/commit/f7c27a807b27cf74cf4666ffe3df4d64cc3f47b3.

Resolving patch:
diff --git a/rasterio/errors.py b/rasterio/errors.py
--- a/rasterio/errors.py
+++ b/rasterio/errors.py
@@ -98,3 +98,7 @@ class WarpOptionsError(RasterioError):
 
 class UnsupportedOperation(RasterioError):
     """Raised when reading from a file opened in 'w' mode"""
+
+
+class OverviewCreationError(RasterioError):
+    """Raised when creation of an overview fails"""


Test patch:
diff --git a/tests/test_overviews.py b/tests/test_overviews.py
--- a/tests/test_overviews.py
+++ b/tests/test_overviews.py
@@ -9,6 +9,8 @@
 import rasterio
 from rasterio.enums import Resampling
 from rasterio.env import GDALVersion
+from rasterio.errors import OverviewCreationError
+
 
 gdal_version = GDALVersion()
 
@@ -79,3 +81,12 @@ def test_test_unsupported_algo(data):
         with rasterio.open(inputfile, 'r+') as src:
             overview_factors = [2, 4]
             src.build_overviews(overview_factors, resampling=Resampling.q1)
+
+
+def test_issue1333(data):
+    """Fail if asked to create more than one 1x1 overview"""
+    inputfile = str(data.join('RGB.byte.tif'))
+    with pytest.raises(OverviewCreationError):
+        with rasterio.open(inputfile, 'r+') as src:
+            overview_factors = [1024, 2048]
+            src.build_overviews(overview_factors, resampling=Resampling.average)