Skip to content

Commit

Permalink
Disable stdout printing
Browse files Browse the repository at this point in the history
1. Performance:
○ Avoid calling srand() multiple times in a program. Instead, call it once at the startup. Re-seeding with GetSystemTimePreciseAsFileTime should be done outside of repetitive scenarios unless necessary for specific randomness properties.

2. Readable and well-structured code:
○ Avoid using magic numbers. Use OUTPUT_LENGTH directly.
○ The initialization syntax { [OUTPUT_LENGTH] = '\1' } is non-standard and could cause confusion. Simplify it to char random_string[OUTPUT_LENGTH + 1]; memset(random_string, '\0', OUTPUT_LENGTH + 1);.
○ Consider separating concerns better by implementing a dedicated function for clipboard operations.
3. Security:
○ Use a stronger random generator for any cryptographic or highly sensitive random generation needs. The rand() function is not cryptographically secure.
4. Potential Bugs:
○ NULL check after GlobalLock() without handling memPtr being NULL before memcpy.
○ Ensure to clear and free any global memory objects properly to avoid memory leaks in case of partial failures.
5. Pattern Usage:
○ Usage of modern C libraries or wrappers for clipboard and random number generation might improve maintainability.
By implementing these suggestions, the code will be more efficient, readable, reliable, and secure.
  • Loading branch information
pierro42 committed Nov 2, 2024
1 parent 890586f commit b4f07c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
55 changes: 42 additions & 13 deletions random_clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OUTPUT_LENGTH 16

void rand_str(char*, size_t);


void rand_str(char* dest, size_t length) {
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
srand((unsigned int)time(NULL));
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"-_";

while (length-- > 0) {


//size_t index = (double)rand() / RAND_MAX * (sizeof charset - 1);
size_t index = (size_t)((double)rand() / RAND_MAX * sizeof(charset));
size_t index = rand() % (sizeof(charset) -1);
*dest++ = charset[index];
}
*dest = '\0';
Expand All @@ -29,21 +29,50 @@ void rand_str(char* dest, size_t length) {

int main()
{
char str[17] = { [16] = '\1' }; // make the last character non-zero so we can test based on it later
rand_str(str, sizeof str - 1);
assert(str[16] == '\0'); // test the correct insertion of string terminator
puts(str);

FILETIME ft;
ULARGE_INTEGER ui;
GetSystemTimePreciseAsFileTime(&ft);
ui.LowPart = ft.dwLowDateTime;
ui.HighPart = ft.dwHighDateTime;
unsigned long long micros = ui.QuadPart / 10;
srand((unsigned int) (micros & 0xFFFFFFFF));

char random_string[OUTPUT_LENGTH +1] = { [OUTPUT_LENGTH] = '\1' }; // make the last character non-zero so we can test based on it later
rand_str(random_string, sizeof random_string - 1);
assert(random_string[OUTPUT_LENGTH] == '\0'); // test the correct insertion of string terminator

// const char* output = "Test";
const size_t len = strlen(str) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), str, len);

const size_t random_string_len = strlen(random_string) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, random_string_len);

if (hMem == NULL)
{
fprintf(stderr, "GlobalAlloc failed \n");
return 1;
}


char* memPtr = (char*)GlobalLock(hMem);

if (memPtr != NULL) {
memcpy(memPtr, random_string, random_string_len);
}
else {
fprintf(stderr, "GlobalLock failed\n");
GlobalFree(hMem); // Free allocated memory on failure
return 1;

}



GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();

}

// Exécuter le programme : Ctrl+F5 ou menu Déboguer > Exécuter sans débogage
Expand Down
2 changes: 1 addition & 1 deletion random_clipboard.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
</Link>
Expand Down

0 comments on commit b4f07c3

Please sign in to comment.