sanitize: add bipmkpw_fatal & fix bipmkpw warnings

This commit is contained in:
Loïc Gomez 2022-01-06 19:48:59 +01:00 committed by Pierre-Louis Bonicoli
parent fe108e390f
commit 26d34bafab
Signed by: pilou
GPG Key ID: 06914C4A5EDAA6DD
1 changed files with 34 additions and 17 deletions

View File

@ -2,7 +2,8 @@
* $Id$ * $Id$
* *
* This file is part of the bip project * This file is part of the bip project
* Copyright (C) 2004 2005 Arnaud Cornet and Loïc Gomez * Copyright (C) 2004,2005 Arnaud Cornet
* Copyright (C) 2004,2005,2022 Loïc Gomez
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -26,34 +27,42 @@ int conf_log_level;
FILE *conf_global_log_file; FILE *conf_global_log_file;
int conf_log_system; int conf_log_system;
void bipmkpw_fatal(char *msg, char *err)
{
fprintf(stderr, "%s: %s\n", msg, err);
exit(1);
}
void readpass(char *buffer, int buflen) void readpass(char *buffer, int buflen)
{ {
int ttyfd = open("/dev/tty", O_RDWR); int ttyfd = open("/dev/tty", O_RDWR);
if (ttyfd == -1) { if (ttyfd == -1)
fprintf(stderr, "Unable to open tty: %s\n", strerror(errno)); bipmkpw_fatal("Unable to open tty", strerror(errno));
exit(1);
}
struct termios tt, ttback; struct termios tt, ttback;
memset(&ttback, 0, sizeof(ttback)); memset(&ttback, 0, sizeof(ttback));
if (tcgetattr(ttyfd, &ttback) < 0) { if (tcgetattr(ttyfd, &ttback) < 0)
fprintf(stderr, "tcgetattr failed: %s\n", strerror(errno)); bipmkpw_fatal("tcgetattr failed", strerror(errno));
exit(1);
}
memcpy(&tt, &ttback, sizeof(ttback)); memcpy(&tt, &ttback, sizeof(ttback));
// unsigned conversion from int to tcflag_t {aka unsigned int} changes value from -11 to 4294967285
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
tt.c_lflag &= ~(ICANON|ECHO); tt.c_lflag &= ~(ICANON|ECHO);
if (tcsetattr(ttyfd, TCSANOW, &tt) < 0) { #pragma GCC diagnostic pop
fprintf(stderr, "tcsetattr failed: %s\n", strerror(errno)); if (tcsetattr(ttyfd, TCSANOW, &tt) < 0)
exit(1); bipmkpw_fatal("tcsetattr failed", strerror(errno));
}
write(ttyfd, "Password: ", 10); if (!write(ttyfd, "Password: ", (size_t)10))
bipmkpw_fatal("tty write failed", strerror(errno));
int idx = 0; int idx = 0;
int valid = 1; int valid = 1;
while (idx < buflen) { while (idx < buflen) {
read(ttyfd, buffer+idx, 1); ssize_t rbytes = read(ttyfd, buffer+idx, (size_t)1);
if (rbytes <= 0) {
break;
}
if (buffer[idx] == '\n') { if (buffer[idx] == '\n') {
buffer[idx] = 0; buffer[idx] = 0;
break; break;
@ -63,7 +72,8 @@ void readpass(char *buffer, int buflen)
idx++; idx++;
} }
write(ttyfd, "\n", 1); if (!write(ttyfd, "\n", (size_t)1))
bipmkpw_fatal("tty write failed", strerror(errno));
tcsetattr(ttyfd, TCSANOW, &ttback); tcsetattr(ttyfd, TCSANOW, &ttback);
close(ttyfd); close(ttyfd);
@ -84,9 +94,16 @@ int main(void)
readpass(str, 256); readpass(str, 256);
str[255] = 0; str[255] = 0;
// passing argument 1 of srand with different width due to prototype [-Werror=traditional-conversion]
// conversion from time_t {aka long int} to unsigned int may change value [-Werror=conversion]
// We don't care.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtraditional-conversion"
#pragma GCC diagnostic ignored "-Wconversion"
// the time used to type the pass is entropy // the time used to type the pass is entropy
srand(time(NULL)); srand(time(NULL));
seed = rand(); #pragma GCC diagnostic pop
seed = (unsigned)rand(); // rand should be > 0
md5 = chash_double(str, seed); md5 = chash_double(str, seed);
for (i = 0; i < 20; i++) for (i = 0; i < 20; i++)