Fix kernel-mode executive atom implementation (mostly add SEH and tidy up the code...
[reactos.git] / reactos / lib / rtl / atom.c
index 67ed9a5..1c643df 100644 (file)
@@ -135,7 +135,7 @@ RtlpCheckIntegerAtom(PWSTR AtomName,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlCreateAtomTable(IN ULONG TableSize,
                    IN OUT PRTL_ATOM_TABLE *AtomTable)
 {
@@ -150,6 +150,9 @@ RtlCreateAtomTable(IN ULONG TableSize,
         return STATUS_SUCCESS;
      }
 
+   /* Use default if size was incorrect */
+   if (TableSize <= 1) TableSize = 37;
+
    /* allocate atom table */
    Table = RtlpAllocAtomTable(((TableSize - 1) * sizeof(PRTL_ATOM_TABLE_ENTRY)) +
                               sizeof(RTL_ATOM_TABLE));
@@ -183,7 +186,7 @@ RtlCreateAtomTable(IN ULONG TableSize,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlDestroyAtomTable(IN PRTL_ATOM_TABLE AtomTable)
 {
    PRTL_ATOM_TABLE_ENTRY *CurrentBucket, *LastBucket;
@@ -232,7 +235,7 @@ RtlDestroyAtomTable(IN PRTL_ATOM_TABLE AtomTable)
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlEmptyAtomTable(PRTL_ATOM_TABLE AtomTable,
                   BOOLEAN DeletePinned)
 {
@@ -286,7 +289,7 @@ RtlEmptyAtomTable(PRTL_ATOM_TABLE AtomTable,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable,
                       IN PWSTR AtomName,
                       OUT PRTL_ATOM Atom)
@@ -347,6 +350,12 @@ RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable,
         if (HashLink != NULL)
           {
              ULONG AtomNameLen = wcslen(AtomName);
+             
+             if (AtomNameLen > RTL_MAXIMUM_ATOM_LENGTH)
+             {
+                Status = STATUS_INVALID_PARAMETER;
+                goto end;
+             }
 
              Entry = RtlpAllocAtomTableEntry(sizeof(RTL_ATOM_TABLE_ENTRY) -
                                              sizeof(Entry->Name) +
@@ -390,7 +399,7 @@ RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable,
              Status = STATUS_OBJECT_NAME_INVALID;
           }
      }
-
+end:
    RtlpUnlockAtomTable(AtomTable);
 
    return Status;
@@ -400,7 +409,7 @@ RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlDeleteAtomFromAtomTable(IN PRTL_ATOM_TABLE AtomTable,
                            IN RTL_ATOM Atom)
 {
@@ -472,7 +481,7 @@ RtlDeleteAtomFromAtomTable(IN PRTL_ATOM_TABLE AtomTable,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlLookupAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
                          IN PWSTR AtomName,
                          OUT PRTL_ATOM Atom)
@@ -501,27 +510,22 @@ RtlLookupAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
      }
 
    RtlpLockAtomTable(AtomTable);
-
    Status = STATUS_OBJECT_NAME_NOT_FOUND;
 
    /* string atom */
    Entry = RtlpHashAtomName(AtomTable,
                             AtomName,
                             &HashLink);
-
    if (Entry != NULL)
      {
         Status = STATUS_SUCCESS;
         FoundAtom = (RTL_ATOM)Entry->Atom;
      }
-
    RtlpUnlockAtomTable(AtomTable);
-
    if (NT_SUCCESS(Status) && Atom != NULL)
      {
         *Atom = FoundAtom;
      }
-
    return Status;
 }
 
@@ -529,7 +533,7 @@ RtlLookupAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlPinAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
                       IN RTL_ATOM Atom)
 {
@@ -583,7 +587,7 @@ RtlPinAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
  * (again EXCLUDING the null terminator) is returned in NameLength, at least
  * on Win2k, XP and ReactOS. NT4 will return 0 in that case.
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable,
                         RTL_ATOM Atom,
                         PULONG RefCount,
@@ -592,6 +596,8 @@ RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable,
                         PULONG NameLength)
 {
    ULONG Length;
+   BOOL Unlock = FALSE;
+   
    union
      {
      /* A RTL_ATOM_TABLE_ENTRY has a "WCHAR Name[1]" entry at the end.
@@ -616,6 +622,7 @@ RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable,
    else
      {
         RtlpLockAtomTable(AtomTable);
+        Unlock = TRUE;
 
         Entry = RtlpGetAtomEntry(AtomTable,
                                  (ULONG)((USHORT)Atom - 0xC000));
@@ -676,10 +683,7 @@ RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable,
         Status = STATUS_INVALID_HANDLE;
      }
 
-   if (NULL != Entry && Entry != &NumberEntry.AtomTableEntry)
-     {
-        RtlpUnlockAtomTable(AtomTable);
-     }
+   if (Unlock) RtlpUnlockAtomTable(AtomTable);
 
    return Status;
 }
@@ -688,7 +692,7 @@ RtlQueryAtomInAtomTable(PRTL_ATOM_TABLE AtomTable,
 /*
  * @private - only used by NtQueryInformationAtom
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlQueryAtomListInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
                             IN ULONG MaxAtomCount,
                             OUT ULONG *AtomCount,