I have this arraylist implementation working fine in usermode, when i tried pass to kernel mode version i receive a bsod on this part:

Code:
FAULTING_SOURCE_LINE_NUMBER:  128
 
FAULTING_SOURCE_CODE:  
   124: {
   125:     int index = 0;
   126:     while (index <= list->current)
   127:     {
>  128:      if (e->data->Length == list->elements[index].data->Length &&
   129:             0 == wcsncmp(e->data->Buffer,
   130:                 list->elements[index].data->Buffer,
   131:                 list->elements[index].data->Length))
   132:             return index;
   133:         index++;
kernel mode version:

Code:
NTSTATUS NTAPI RtlDuplicateUnicodeString(IN ULONG Flags, IN PCUNICODE_STRING SourceString, OUT PUNICODE_STRING DestinationString);

typedef unsigned char uint8_t;
 
typedef struct
{
    UNICODE_STRING *data;
}Element;
 
typedef struct
{
    int current;
    int size;
    int increment_rate;
    Element *elements;
}ArrayList;
 
void FreeUString(UNICODE_STRING *src)
{
    RtlFreeUnicodeString(src);
    src->Length = src->MaximumLength = 0;
}
 
void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
{
    list->size = size;
    list->increment_rate = rate;
    list->elements = (Element*)ExAllocatePoolWithTag(NonPagedPool, sizeof(Element), 'Fo');
    list->current = -1;
}
 
void initWithSize(ArrayList *const list, int size)
{
    initWithSizeAndIncRate(list, size, 50);
}
 
void init(ArrayList *const list)
{
    initWithSize(list, 100);
}
 
void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
{
    uint8_t *udest = (uint8_t*)dest;
    uint8_t *usrc = (uint8_t*)src;
    dIndex *= size;
    sIndex *= size;
    len *= size;
    destLen *= size;
 
    if (src != dest)
    {
        memcpy(&udest[dIndex], &usrc[sIndex], len);
    }
    else
    {
        if (dIndex > sIndex)
        {
            uint8_t *tmp = (uint8_t*)ExAllocatePoolWithTag(NonPagedPool, size, 'F');
            memcpy(tmp, &udest[dIndex], (destLen - dIndex));
            memcpy(&udest[dIndex], &usrc[sIndex], len);
            memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
            ExFreePoolWithTag(tmp, 'F');
        }
        else if (sIndex > dIndex)
        {
            memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
        }
        else
            return;
    }
}
 
void clear(ArrayList *const list)
{
    while (list->current >= 0)
    {
        FreeUString(list->elements[list->current].data);
        list->current--;
    }
}
 
void wide(ArrayList* const list)
{
    list->size += list->increment_rate;
    Element *newArr = (Element*)ExAllocatePoolWithTag(NonPagedPool, sizeof(Element), 'T');
    arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
    //ExFreePoolWithTag(list->elements, 'Foo');
    list->elements = newArr;
}
 
int add(ArrayList *const list, Element *e)
{
    UNICODE_STRING *dest = { NULL };
    NTSTATUS status = STATUS_SUCCESS;
 
    if (++list->current < list->size)
    {
        status = RtlDuplicateUnicodeString(1, e->data, dest);
        DbgPrint("RtlDuplicateUnicodeString() status: 0x%x", status);
 
        list->elements[list->current].data = dest;
        return 1;
    }
    else
    {
        wide(list);
        status = RtlDuplicateUnicodeString(1, e->data, dest);
        DbgPrint("RtlDuplicateUnicodeString() status: 0x%x", status);
        list->elements[list->current].data = dest;
        return 1;
    }
    return 0;
}
 
int indexOf(const ArrayList *const list, Element *e)
{
    int index = 0;
    while (index <= list->current)
    {
        if (e->data->Length == list->elements[index].data->Length &&
            0 == wcsncmp(e->data->Buffer,
                list->elements[index].data->Buffer,
                list->elements[index].data->Length))
            return index;
        index++;
    }
    return 0;
}
 
void clean(ArrayList *list)
{
    ExFreePoolWithTag(list->elements, 'Fo');
}
 
ArrayList list;
Element e;
usermode version:

Code:
typedef struct
{
    UNICODE_STRING *data;
}Element;
 
typedef struct
{
    int current;
    int size;
    int increment_rate;
    Element *elements;
}ArrayList;
 
UNICODE_STRING * CopyUString(UNICODE_STRING *src)
{
    UNICODE_STRING *dest = (UNICODE_STRING *)malloc(sizeof(UNICODE_STRING));
    dest->Length = src->Length;
    dest->MaximumLength = src->MaximumLength;
    dest->Buffer = (PWSTR)malloc(sizeof(WCHAR) * dest->MaximumLength);
    memcpy(dest->Buffer, src->Buffer, sizeof(WCHAR) * dest->MaximumLength);
    return dest;
}
 
void FreeUString(UNICODE_STRING *src)
{
    free(src->Buffer);
    src->Length = src->MaximumLength = 0;
}
 
void initWithSizeAndIncRate(ArrayList *const list, int size, int rate)
{
    list->size = size;
    list->increment_rate = rate;
    list->elements = (Element*)calloc(sizeof(Element), list->size);
    list->current = -1;
}
 
void initWithSize(ArrayList *const list, int size)
{
    initWithSizeAndIncRate(list, size, 50);
}
 
void init(ArrayList *const list)
{
    initWithSize(list, 100);
}
 
void arraryCopy(void *dest, int dIndex, const void* src, int sIndex, int len, int destLen, size_t size)
{
    uint8_t *udest = (uint8_t*)dest;
    uint8_t *usrc = (uint8_t*)src;
    dIndex *= size;
    sIndex *= size;
    len *= size;
    destLen *= size;
 
    if (src != dest)
    {
        memcpy(&udest[dIndex], &usrc[sIndex], len);
    }
    else
    {
        if (dIndex > sIndex)
        {
            uint8_t *tmp = (uint8_t*)calloc(destLen, size);
            memcpy(tmp, &udest[dIndex], (destLen - dIndex));
            memcpy(&udest[dIndex], &usrc[sIndex], len);
            memcpy(&udest[dIndex + len], tmp, (destLen - dIndex));
            free(tmp);
        }
        else if (sIndex > dIndex)
        {
            memcpy(&udest[dIndex], &usrc[sIndex], (destLen - sIndex) + 1);
        }
        else
            return;
    }
}
 
void clear(ArrayList *const list)
{
    while (list->current >= 0)
    {
        FreeUString(list->elements[list->current].data);
        list->current--;
    }
}
 
void wide(ArrayList* const list)
{
    list->size += list->increment_rate;
    Element *newArr = (Element*)calloc(sizeof(Element), list->size);
    arraryCopy(newArr, 0, list->elements, 0, list->current, list->size, sizeof(Element));
    free(list->elements);
    list->elements = newArr;
}
 
int add(ArrayList *const list, Element *e)
{
    if (++list->current < list->size)
    {
        list->elements[list->current].data = CopyUString(e->data);
        return 1;
    }
    else
    {
        wide(list);
        list->elements[list->current].data = CopyUString(e->data);
        return 1;
    }
    return 0;
}
 
int indexOf(const ArrayList *const list, Element *e)
{
    int index = 0;
    while (index <= list->current)
    {
        if (e->data->Length == list->elements[index].data->Length &&
            0 == wcsncmp(e->data->Buffer,
            list->elements[index].data->Buffer,
            list->elements[index].data->Length))
            return index;
        index++;
    }
    return 0;
}
 
void clean(ArrayList *list)
{
    free(list->elements);
}
Some idea about what's causing this bsod? thanks in advance.