Skip to content

Commit

Permalink
Merge pull request #40 from Golit/master
Browse files Browse the repository at this point in the history
Fix crash caused by accessing null file pointer
  • Loading branch information
Andy2244 authored Apr 25, 2022
2 parents 779f3e3 + fbd80d7 commit e37443a
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions wsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@

#include <stdbool.h> // bool
#include <stdio.h> // FILE, fopen(), fscanf(), snprintf(), asprintf()
#include <stdlib.h> // srand48()
#include <stdlib.h> // srand48(), strtoul()
#include <unistd.h> // usleep()
#include <string.h> // strcmp(), strdup()
#include <string.h> // strcmp(), strdup(), strncpy()
#include <ctype.h> // isdigit(), isspace()
#include <time.h> // time_t, time()
#include <errno.h> // errno
Expand All @@ -71,23 +71,37 @@
static time_t wsd_instance;
static char wsd_sequence[UUIDLEN], wsd_endpoint[UUIDLEN];

static void uuid_endpoint(char uuid[UUIDLEN]);

static int uuid_parse(char uuid[UUIDLEN], unsigned short UUID[8])
{
size_t pos[] = {0,4,9,14,19,24,28,32};
for(size_t i = 0; i < 8; ++i) {
char str[5];
strncpy(str, uuid+pos[i], 4);
str[4] = '\0';
unsigned long s = strtoul(str, NULL, 16);
UUID[i] = s;
}
return 0;
}

static void set_seed(void)
{
FILE *fp = fopen("/etc/machine-id", "r");
char uuid[UUIDLEN];
unsigned short UUID[8];
unsigned long seed;

time((time_t *)&seed);

fseek(fp, 0, SEEK_END);
uuid_endpoint(uuid);
uuid_parse(uuid, UUID);

if (fp && 0 != ftell(fp) ) {
unsigned long s;
rewind(fp);

while (fscanf(fp, "%8lx", &s) > 0)
seed ^= s;
fclose(fp);
time((time_t *)&seed);

for(size_t i = 0; i < 4; ++i) {
unsigned long s = UUID[2*i+1] | UUID[2*i+0] << 16;
seed ^= s;
}

srand48(seed);
}

Expand Down

0 comments on commit e37443a

Please sign in to comment.