From e707f454ed1a5c1cd47eedc44f5d50d06e58c1ad Mon Sep 17 00:00:00 2001 From: mathias Date: Mon, 11 Sep 2006 21:31:18 +0000 Subject: [PATCH] * made the timeout function more clear * only accept rlen < 50, dont increase it when it makes no sense * minor splint based mods --HG-- extra : convert_revision : svn%3Aeebe1cee-a9af-4fe4-bd26-ad572b19c5ab/trunk%4046 --- src/alock.c | 43 ++++++++++++++++++++++++------------------- src/auth_sha2.c | 47 +++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/alock.c b/src/alock.c index 2b48857..7a7caa5 100644 --- a/src/alock.c +++ b/src/alock.c @@ -122,7 +122,7 @@ static void displayUsage() { /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ -static void initXInfo(struct aXInfo* xinfo, struct aOpts* opts) { +static void initXInfo(struct aXInfo* xinfo) { Display* dpy = XOpenDisplay(NULL); @@ -132,18 +132,17 @@ static void initXInfo(struct aXInfo* xinfo, struct aOpts* opts) { } { - const size_t nr_screens = ScreenCount(dpy); xinfo->display = dpy; - xinfo->nr_screens = nr_screens; - xinfo->window = (Window*)calloc(nr_screens, sizeof(Window)); - xinfo->root = (Window*)calloc(nr_screens, sizeof(Window)); - xinfo->colormap = (Colormap*)calloc(nr_screens, sizeof(Colormap)); - xinfo->cursor = (Cursor*)calloc(nr_screens, sizeof(Cursor)); + xinfo->nr_screens = ScreenCount(dpy); + xinfo->window = (Window*)calloc((size_t)xinfo->nr_screens, sizeof(Window)); + xinfo->root = (Window*)calloc((size_t)xinfo->nr_screens, sizeof(Window)); + xinfo->colormap = (Colormap*)calloc((size_t)xinfo->nr_screens, sizeof(Colormap)); + xinfo->cursor = (Cursor*)calloc((size_t)xinfo->nr_screens, sizeof(Cursor)); } { int scr; for (scr = 0; scr < xinfo->nr_screens; scr++) { - xinfo->window[scr] = 0; + xinfo->window[scr] = None; xinfo->root[scr] = RootWindow(dpy, scr); xinfo->colormap[scr] = DefaultColormap(dpy, scr); } @@ -157,7 +156,8 @@ static int event_loop(struct aOpts* opts, struct aXInfo* xinfo) { char cbuf[10], rbuf[50]; unsigned int clen, rlen = 0; - long goodwill = 5 * 30000; + const long max_goodwill = 5 * 30000; /* 150 seconds */ + long goodwill = max_goodwill; long timeout = 0; for(;;) { @@ -194,22 +194,29 @@ static int event_loop(struct aOpts* opts, struct aXInfo* xinfo) { XSync(xinfo->display, True); /* discard pending events to start really fresh */ XBell(xinfo->display, 0); rlen = 0; + if (timeout) { goodwill += ev.xkey.time - timeout; - if (goodwill > 5 * 30000) { - goodwill = 5 * 30000; + if (goodwill > max_goodwill) { + goodwill = max_goodwill; } } - timeout = -goodwill * 0.3; - goodwill += timeout; - timeout += ev.xkey.time + 30000; + + { + long offset; + + offset = goodwill * 0.3; + goodwill = goodwill - offset; + timeout = ev.xkey.time + 30000 - offset; + } break; default: if (clen != 1) break; - if (rlen < sizeof(rbuf)) + if (rlen < (sizeof(rbuf) - 1)) { rbuf[rlen] = cbuf[0]; - rlen++; + rlen++; + } break; } break; @@ -359,9 +366,7 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - opts.auth->init(NULL); - - initXInfo(&xinfo, &opts); + initXInfo(&xinfo); if (opts.background->init(background_args, &xinfo) == 0) { printf("alock: error, couldnt init [%s] with [%s].\n", diff --git a/src/auth_sha2.c b/src/auth_sha2.c index 23e442e..6a50838 100644 --- a/src/auth_sha2.c +++ b/src/auth_sha2.c @@ -875,6 +875,13 @@ static unsigned int method = 0; static char* method_string = NULL; static size_t method_digest_string_length = 0; +enum { + NONE = 0, + SHA256 = 256, + SHA384 = 384, + SHA512 = 512 +}; + static int alock_auth_sha2_init(const char* args) { if (!args) { @@ -883,16 +890,16 @@ static int alock_auth_sha2_init(const char* args) { } if (strstr(args, "sha256:") == args) { - method = 256; - method_string = "sha256"; + method = SHA256; + method_string = strdup("sha256"); method_digest_string_length = SHA256_DIGEST_STRING_LENGTH; } else if (strstr(args, "sha512:") == args) { - method = 512; - method_string = "sha512"; + method = SHA512; + method_string = strdup("sha512"); method_digest_string_length = SHA512_DIGEST_STRING_LENGTH; } else if (strstr(args, "sha384:") == args) { - method = 384; - method_string = "sha384"; + method = SHA384; + method_string = strdup("sha384"); method_digest_string_length = SHA384_DIGEST_STRING_LENGTH; } else { printf("alock: error, not supported hash in [sha2].\n"); @@ -933,6 +940,7 @@ static int alock_auth_sha2_init(const char* args) { } else { printf("alock: error, couldnt read [%s] for [%s].\n", &arg[5], method_string); + free(method_string); free(arguments); return 0; } @@ -940,6 +948,7 @@ static int alock_auth_sha2_init(const char* args) { if (!tmp_hash || strlen(tmp_hash) != method_digest_string_length - 1) { printf("alock: error, given file [%s] doesnt contain a valid hash for [%s].\n", &arg[5], method_string); + free(method_string); free(arguments); return 0; } @@ -951,11 +960,13 @@ static int alock_auth_sha2_init(const char* args) { free(arguments); } else { fprintf(stderr, "alock: error, missing arguments for [%s].\n", method_string); + free(method_string); return 0; } if (!userhash) { printf("alock: error, missing hash for [%s].\n", method_string); + free(method_string); return 0; } @@ -965,11 +976,15 @@ static int alock_auth_sha2_init(const char* args) { } static int alock_auth_sha2_deinit() { + if (userhash) free(userhash); + userhash = NULL; + if (method_string) + free(method_string); method_string = NULL; - method = 0; + method = NONE; method_digest_string_length = 0; return 1; } @@ -980,7 +995,7 @@ static int alock_auth_sha2_auth(const char* pass) { return 0; switch (method) { - case 256: { + case SHA256: { unsigned char digest[SHA256_DIGEST_LENGTH]; unsigned char stringdigest[SHA256_DIGEST_STRING_LENGTH]; unsigned int i; @@ -997,7 +1012,7 @@ static int alock_auth_sha2_auth(const char* pass) { return !strcmp((char*)stringdigest, userhash); } break; - case 512: { + case SHA512: { unsigned char digest[SHA512_DIGEST_LENGTH]; unsigned char stringdigest[SHA512_DIGEST_STRING_LENGTH]; unsigned int i; @@ -1014,7 +1029,7 @@ static int alock_auth_sha2_auth(const char* pass) { return !strcmp((char*)stringdigest, userhash); } break; - case 384: { + case SHA384: { unsigned char digest[SHA384_DIGEST_LENGTH]; unsigned char stringdigest[SHA384_DIGEST_STRING_LENGTH]; unsigned int i; @@ -1079,14 +1094,14 @@ int main(int argc, char* argv[]) { if (argc < 2) { usage(); - exit(0); + exit(EXIT_SUCCESS); } method = atoi(argv[1]); switch (method) { - case 256: { + case SHA256: { sha256Context sha256; sha256_init(&sha256); while((c = fgetc(stdin)) != (unsigned char)EOF) { @@ -1096,7 +1111,7 @@ int main(int argc, char* argv[]) { method_digest_length = SHA256_DIGEST_LENGTH; } break; - case 384: { + case SHA384: { sha384Context sha384; sha384_init(&sha384); while((c = fgetc(stdin)) != (unsigned char)EOF) { @@ -1106,7 +1121,7 @@ int main(int argc, char* argv[]) { method_digest_length = SHA384_DIGEST_LENGTH; } break; - case 512: { + case SHA512: { sha512Context sha512; sha512_init(&sha512); while((c = fgetc(stdin)) != (unsigned char)EOF) { @@ -1118,7 +1133,7 @@ int main(int argc, char* argv[]) { break; default: usage(); - exit(1); + exit(EXIT_FAILURE); break; }; @@ -1127,7 +1142,7 @@ int main(int argc, char* argv[]) { printf("\n"); fflush(stdout); - return 0; + return EXIT_SUCCESS; } #endif /* STAND_ALONE */