Will memset vector to 0 cause memory leak? - memory-leaks

I have a structure as followed:
typedef enum tBrowseType
{
catNowPlaying,
catFolder,
catFile,
catGenre
};
typedef struct tBrowseList
{
int total;
vector<tBrowseType>browseType;
};
After the initial use, I did a memset; will this memset cause memory leak?

If you've zeroed the vector<tBrowseType> object it is more likely to cause segfault. memset is not the way to treat decent c++ object.

Related

Confused use of memcpy in ext2fs library

I am reading the source code implementation of libext2fs, which is part of the project e2fsprogs used to debug Ext2/3/4 file systems. And came across one confused point about the use of method memcpy as follows.
In the library, it maintains one function ext2fs_get_mem which is used to allocate dynamic memories:
_INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
{
void *pp;
pp = malloc(size);
if (!pp)
return EXT2_ET_NO_MEMORY;
memcpy(ptr, &pp, sizeof (pp));
return 0;
}
The caller will call it like:
retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs)
In the above case, variable fs is just type of struct struct_ext2_filsys, all right.
The confused point is why the function ext2fs_get_mem need to call memcpy and what's the purpose? Why not directly allocate memory to the pointer void *ptr by malloc?

Helping with void *__ctx[] CRYPTO_MINALIGN_ATTR in struct aead_request (Crypto Subsystem in Linux Kernel)

I'm a newbie in Linux driver. Now, I am testing my Linux crypto driver. However, I have some kernel crash bug related to this pointer array. In my opinion, this pointer array is related to a Variable Length Array In Struct (VLAIS). I did some research about how void *__ctx[] CRYPTO_MINALIGN_ATTR work but still didn't understand. Can anyone help me clarify this type and how it work? Thank in advance.
This is the location of this type (include/crypto/aead.h):
struct aead_request {
struct crypto_async_request base;
unsigned int assoclen;
unsigned int cryptlen;
u8 *iv;
struct scatterlist *src;
struct scatterlist *dst;
void *__ctx[] CRYPTO_MINALIGN_ATTR;
};

i am creating a kernel module to find the resident pages for all the process

I am creating a kernel module to find the resident pages for all the process. I am using get_mm_rss() and for_each_process but it works
only for init process / first time after first iteration it doesn't work.
int __init schedp(void){
struct task_struct *p;
for_each_process(p) {
int pid = task_pid_nr(p);
printk("Process: %s (pid = %d) , (rpages : %lu)\n",
p->comm, pid, get_mm_rss(p->mm));
}
return 0;
}
Results:
BUG: unable to handle kernel NULL pointer dereference at 00000160,
You're probably getting NULL in p->mm, because some tasks may have invalid mm pointer because they are exiting or don't have mm (because they are kernel-threads, not sure).
When you confused on how to use kernel API, always look for examples inside kernel itself. Quick search with cross-reference tool gave me kernel/cpu.c:
for_each_process(p) {
struct task_struct *t;
/*
* Main thread might exit, but other threads may still have
* a valid mm. Find one.
*/
t = find_lock_task_mm(p);
if (!t)
continue;
cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
task_unlock(t);
}
Note that you need to call find_lock_task_mm() and task_unlock() and explicitly check for NULL.
finally it works after creating a function which checks a mm_struct is valid or not for_each_process
struct task_struct *task_mm(struct task_struct *p){
struct task_struct *t;
rcu_read_lock();
for_each_thread(p, t) {
task_lock(t);
if (likely(t->mm))
goto found;
task_unlock(t);
}
t = NULL;
found:
rcu_read_unlock();
return t;
}

Linux kernel datastructures

I am new to module writing and need a circular buffer[1] and a vector. Since the Linux kernel apparently provides some data structures (lib) (list, trees), I was wondering if there is a vector equivalent?
While I think I am well capable to write my own, I prefer libraries for such things to prevent the duplication of code and to avoid errors.
[1] Found while composing the question, kfifo, also Queues in the Linux Kernel might be of interest.
As far as I know there is no implementation of vectors till 4.1 Linux kernel. And it does not make any sense to have one as vectors can be designed with the basic data structures whose implementation is already provided in Linux kernel.
I'm a bit late, but if anyone needs to implement a generalized vector in C, it can be done using void pointers and a sizeof operator on initialization. It would look something like this:
struct MyVec {
void *data;
size_t stored_sizeof;
size_t size;
size_t allocated_size
};
#define STARTING_ALLOCATED_SIZE 10
void MyVec_init(struct MyVec *self, size_t sizeof_data_type) {
self->stored_sizeof = sizeof_data_type;
self->data = malloc(self->stored_sizeof * STARTING_ALLOCATED_SIZE);
self->size = 0;
self->allocated_size = STARTING_ALLOCATED_SIZE;
}
void MyVec_deinit(struct MyVec *self) {
free(self->data);
}
bool MyVec_at(struct MyVec *self, size_t index, void *to_fill) {
if (index >= size)
return false;
memcpy(to_fill, self->data + (index * self->stored_sizeof), self->stored_sizeof);
return true;
}
and all the other methods that you might need, just being sure to use stored_sizeof whenever indexing, adding/removing elements, and the like.

0xC0000005: Access Violation. when working with a lot of points

I must use an old and crappy VC6.0 software.
This part of code process points, for a kind of 3D printer.
When I got a lot of points, (>2 000 000), it fails with "0xC0000005: Access Violation"
I'm really stuck, I have no idea how to fix this. The code is not from me.
Could it be too much malloc, with memory leaks ?
P_NOEUDP cstr_noeudp()
{
P_NOEUDP N;
N=(P_NOEUDP) malloc(sizeof(NOEUDP));
N->classe=NULL; //Here it is 0xC0000005: Access Violation.
N->face=NULL;
N->arete_isolee=NULL;
N->critere=NULL;
N->data_list=NULL;
return(N);
}
This is called in :
P_SLICE_POINT tpp_Slice_Point_Cstr()
{
P_SLICE_POINT pSPt;
pSPt = (P_SLICE_POINT) malloc(sizeof(SLICE_POINT));
pSPt->Node=cstr_noeudp();
pSPt->Edge=NULL;
return pSPt;
}
Here are the headers :
struct S_NOEUDP
{
int numero;
double courbureGaus;
double X[3];
CLASSEMENT *classe;
LISTE *face;
LISTE *arete_isolee;
LISTE *critere;
P_DATA_LISTE data_list;
};
typedef struct S_NOEUDP NOEUDP;
typedef NOEUDP *P_NOEUDP;
struct S_CLASSEMENT
{
int type;
int etat;
int situation;
};
typedef struct S_CLASSEMENT CLASSEMENT;
typedef CLASSEMENT *P_CLASSEMENT;
struct S_LISTE
{
int type_liste;
int type_occurence;
void *occurence;
int type_reference;
void *reference;
struct S_LISTE *svt;
struct S_LISTE *prec;
};
typedef struct S_LISTE LISTE;
typedef LISTE *P_LISTE;
struct S_DATA_LISTE
{
P_LISTE data_liste;
};
typedef struct S_DATA_LISTE DATA_LISTE ;
typedef DATA_LISTE *P_DATA_LISTE ;
typedef struct S_SLICE_POINT SLICE_POINT;
typedef SLICE_POINT *P_SLICE_POINT;
So, crappy code, full of memory leaks and no error control...
What's happening next :
Memory fills up because of memory leaks (malloc without freeing)
Memory usage reachs the 32 bit limits, could no more allocate
Code hangs, and we don't know why, because there's no malloc return check

Resources