Skip to content

Commit

Permalink
Merge pull request #249 from Esri/tmaurer3/fpl_better_error_handling
Browse files Browse the repository at this point in the history
fpl better error handling
  • Loading branch information
tmaurer3 committed Oct 25, 2023
2 parents 1906bfb + 5625ede commit c7418a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/LercLib/fpl_Compression.cpp
Expand Up @@ -67,10 +67,13 @@ size_t fpl_Compression::compress_buffer(const char* data, size_t size, char** ou

int ret = fpl_EsriHuffman::EncodeHuffman(data, size, (unsigned char**)output, use_rle);

if (ret < 0)
{
#ifdef _DEBUG
//if (ret < 0)
// fprintf(stderr, "Huffman failed. Input size: %zd. Code: %d\n", size, ret);
// fprintf(stderr, "Huffman failed. Input size: %zd. Code: %d\n", size, ret);
#endif
return (size_t)UINT_MAX + 1;
}

return ret;
}
Expand Down
15 changes: 12 additions & 3 deletions src/LercLib/fpl_EsriHuffman.cpp
Expand Up @@ -354,6 +354,10 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
if (rle_len > 0 && (rle_len < numBytes) && (rle_len < (long)input_len))
{
*ppByte = (unsigned char *)malloc (rle_len + 1);

if (*ppByte == NULL)
return MEMORY_ALLOC_FAIL;

unsigned char * originalPtr = *ppByte ;
originalPtr[0] = HUFFMAN_PACKBITS;

Expand All @@ -367,6 +371,10 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
if (numBytes >= (int)input_len) // huffman will take more space than uncompressed. Don't encode.
{
*ppByte = (unsigned char *)malloc (input_len + 1);

if (*ppByte == NULL)
return MEMORY_ALLOC_FAIL;

unsigned char * originalPtr = *ppByte ;
originalPtr[0] = HUFFMAN_NO_ENCODING; // as is flag
memcpy (originalPtr + 1, input, input_len);
Expand All @@ -377,9 +385,7 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
*ppByte = (unsigned char *)malloc (numBytes + 1);

if (*ppByte == NULL)
{
return MEMORY_ALLOC_FAIL;
}
return MEMORY_ALLOC_FAIL;

unsigned char * originalPtr = *ppByte ;

Expand All @@ -392,6 +398,7 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
if (!huffman.SetCodes(m_huffmanCodes) || !huffman.WriteCodeTable(ppByte, 5)) // header and code table
{
free (originalPtr);
*ppByte = NULL;
return HUFF_UNEXPECTED;
}

Expand All @@ -409,6 +416,7 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
if (len <= 0)
{
free (originalPtr);
*ppByte = NULL;
return HUFF_UNEXPECTED;
}

Expand All @@ -417,6 +425,7 @@ int fpl_EsriHuffman::EncodeHuffman (const char *input, size_t input_len, unsigne
if (!Huffman::PushValue(ppByte, bitPos, code, len))
{
free(originalPtr);
*ppByte = NULL;
return HUFF_UNEXPECTED;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/LercLib/fpl_Lerc2Ext.h
Expand Up @@ -54,6 +54,9 @@ class LosslessFPCompression
best_level = 0;
}

outBlockBuffer(const outBlockBuffer&) = delete; // disable copy constructor
outBlockBuffer& operator=(const outBlockBuffer&) = delete; // disable assignment

~outBlockBuffer()
{
free(compressed);
Expand All @@ -70,6 +73,9 @@ class LosslessFPCompression
m_predictor_code = PREDICTOR_UNKNOWN;
}

compressedDataSlice(const compressedDataSlice&) = delete; // disable copy constructor
compressedDataSlice& operator=(const compressedDataSlice&) = delete; // disable assignment

~compressedDataSlice()
{
for (auto b : m_buffers)
Expand Down

0 comments on commit c7418a9

Please sign in to comment.