Memory leak in using smb2_read()?
Sun_Peixing at emc.com
Sun_Peixing at emc.com
Mon Mar 16 15:34:50 GMT 2009
Hi All:
I am suing Samba4, patch6. I am writing a test case that read a lot a
large file using smb2_read.
I understand that the data returned from function smb2_read() is stored
in struct smb2_read.out.data, which is a DATA_BLOB type that is
dynamically allocated inside function smb2_read().
Do we need free the memory used by smb2_read.out.data? I checked the
examples of Samba4, I didn't see any of them free the memory used by
smb2_read.out.data. Will this cause memory leaks?
To put it simple, memory is dynamically allocated inside function
smb2_read(), why there is no free in the samba 4 code where smb2_read is
called?
Following is my code snippet, the struct of smb2_read and function
smb2_read comes with Smaba4.
Thanks advance for any help or information
Peixing
=================================================
NTSTATUS test_smb2_read(){
struct smb2_read r;
int total_data_size_read =0;
while(total_data_size_written < file_size){
ZERO_STRUCT(r);
r.in.file.handle = handle;
r.in.length = 8192;
r.in.offset = total_data_size_read;
status = smb2_read(tree, mem_ctx, &r);
if(!NT_STATUS_IS_OK(status)) {
printf("ERROR: smb2_read failed - %s\n",
nt_errstr(status));
return status;
}
total_data_size_read += r.out.data.length;
}
}
======================================
libcli/raw/interfaces.h
struct smb2_read {
enum smb_read_level level;
struct {
union smb_handle file;
/* static body buffer 48 (0x30) bytes */
/* uint16_t buffer_code; 0x31 = 0x30 + 1 */
uint8_t _pad;
uint8_t reserved;
uint32_t length;
uint64_t offset;
/* struct smb2_handle handle; */
uint32_t min_count;
uint32_t channel;
uint32_t remaining;
/* the docs give no indication of what
these channel variables are for */
uint16_t channel_offset;
uint16_t channel_length;
} in;
struct {
/* static body buffer 16 (0x10) bytes */
/* uint16_t buffer_code; 0x11 = 0x10 + 1 */
/* uint8_t data_ofs; */
/* uint8_t reserved; */
/* uint32_t data_size; */
uint32_t remaining;
uint32_t reserved;
/* dynamic body */
DATA_BLOB data;
} out;
} smb2;
libcli/smb2/read.c
/*
send a read request
*/
struct smb2_request *smb2_read_send(struct smb2_tree *tree, struct
smb2_read *io) {
struct smb2_request *req;
req = smb2_request_init_tree(tree, SMB2_OP_READ, 0x30, true, 0);
if (req == NULL) return NULL;
SCVAL(req->out.body, 0x02, 0); /* pad */
SCVAL(req->out.body, 0x03, 0); /* reserved */
SIVAL(req->out.body, 0x04, io->in.length);
SBVAL(req->out.body, 0x08, io->in.offset);
smb2_push_handle(req->out.body+0x10, &io->in.file.handle);
SIVAL(req->out.body, 0x20, io->in.min_count);
SIVAL(req->out.body, 0x24, io->in.channel);
SIVAL(req->out.body, 0x28, io->in.remaining);
SSVAL(req->out.body, 0x2C, io->in.channel_offset);
SSVAL(req->out.body, 0x2E, io->in.channel_length);
smb2_transport_send(req);
return req;
}
/*
recv a read reply
*/
NTSTATUS smb2_read_recv(struct smb2_request *req,
TALLOC_CTX *mem_ctx, struct smb2_read *io) {
NTSTATUS status;
if (!smb2_request_receive(req) ||
!smb2_request_is_ok(req)) {
return smb2_request_destroy(req);
}
SMB2_CHECK_PACKET_RECV(req, 0x10, true);
status = smb2_pull_o16s32_blob(&req->in, mem_ctx,
req->in.body+0x02, &io->out.data);
if (!NT_STATUS_IS_OK(status)) {
smb2_request_destroy(req);
return status;
}
io->out.remaining = IVAL(req->in.body, 0x08);
io->out.reserved = IVAL(req->in.body, 0x0C);
return smb2_request_destroy(req); }
/*
sync read request
*/
NTSTATUS smb2_read(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct
smb2_read *io) {
struct smb2_request *req = smb2_read_send(tree, io);
return smb2_read_recv(req, mem_ctx, io); }
More information about the samba-technical
mailing list