Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak is possible for UDF array arguments #7817

Closed
dmitry-lipetsk opened this issue Oct 28, 2023 · 4 comments
Closed

Memory leak is possible for UDF array arguments #7817

dmitry-lipetsk opened this issue Oct 28, 2023 · 4 comments

Comments

@dmitry-lipetsk
Copy link
Contributor

dmitry-lipetsk commented Oct 28, 2023

FB v4.0.4.2988

If this line throws

UCHAR* const temp = FB_NEW_POOL(*getDefaultMemoryPool()) UCHAR[static_cast<size_t>(to.dsc_length * n)];

you do not release the allocated memory in 'data'
UCHAR* data = FB_NEW_POOL(*getDefaultMemoryPool()) UCHAR[static_cast<size_t>(array_desc->iad_total_length)];

Bricks for reproducing this problem:

DECLARE EXTERNAL FUNCTION MY_UDF__GET_DIM
 INTEGER BY SCALAR_ARRAY
RETURNS INTEGER BY VALUE
ENTRY_POINT 'fn_get_dim__i4__sa'
MODULE_NAME 'lcpi.test.udf.01.dll'

select MY_UDF__GET_DIM(cast('12311111111111111111111111111111111111111111111111111111111111111111111111111111111' as blob)) from rdb$database

image

Memory releases after disconnect.

@dyemanov dyemanov self-assigned this Oct 31, 2023
@dyemanov dyemanov changed the title [FB4] Memory Leaks Memory leak is possible for UDF array arguments Oct 31, 2023
@dyemanov
Copy link
Member

Please verify the fix and I will port it to other versions.

@dmitry-lipetsk
Copy link
Contributor Author

Good time of day.

Fix looks ok.

image

image

Another simple tests work fine, too.

Source code of test UDF:

#include "source/fn.h"
#include <cstddef>

////////////////////////////////////////////////////////////////////////////////

using SSHORT=std::int16_t;
using SLONG =std::int32_t;
using SCHAR =char;

////////////////////////////////////////////////////////////////////////////////

typedef struct dsc
{
 UCHAR  dsc_dtype;
 SCHAR  dsc_scale;
 USHORT dsc_length;
 SSHORT dsc_sub_type;
 USHORT dsc_flags;
 UCHAR* dsc_address; // Used either as offset in a message or as a pointer
} DSC;

struct scalar_array_desc
{
 DSC   sad_desc;
 SLONG sad_dimensions;
 
 struct sad_repeat
 {
  SLONG sad_lower;
  SLONG sad_upper;
 } sad_rpt[1];
};//struct scalar_array_desc

////////////////////////////////////////////////////////////////////////////////

std::int32_t FBUDF_API fn_get_dim__i4__sa(const scalar_array_desc* const descr)
{
 if(descr->sad_desc.dsc_address==nullptr)
  return 0;

 return descr->sad_dimensions;
}//fn_get_dim__i4__sa

////////////////////////////////////////////////////////////////////////////////

@onlylunix
Copy link

Will there be a fix for version 3.0.11?

@dyemanov
Copy link
Member

dyemanov commented Nov 2, 2023

Will there be a fix for version 3.0.11?

Backported.

dyemanov added a commit that referenced this issue Nov 11, 2023
… there, simpilfy after-exception cleanup too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment