Project

General

Profile

Enhancement #750 » 0004-sanitize-add-bipmkpw_fatal-and-fix-bipmkpw-warnings.patch

Loïc Gomez, 2022-01-10 19:27

View differences:

src/bipmkpw.c
26 26
FILE *conf_global_log_file;
27 27
int conf_log_system;
28 28

  
29
void bipmkpw_fatal(char *msg, char *err)
30
{
31
	fprintf(stderr, "%s: %s\n", msg, err);
32
	exit(1);
33
}
34

  
29 35
void readpass(char *buffer, int buflen)
30 36
{
31 37
	int ttyfd = open("/dev/tty", O_RDWR);
32
	if (ttyfd == -1) {
33
		fprintf(stderr, "Unable to open tty: %s\n", strerror(errno));
34
		exit(1);
35
	}
38
	if (ttyfd == -1)
39
		bipmkpw_fatal("Unable to open tty", strerror(errno));
36 40

  
37 41
	struct termios tt, ttback;
38 42
	memset(&ttback, 0, sizeof(ttback));
39
	if (tcgetattr(ttyfd, &ttback) < 0) {
40
		fprintf(stderr, "tcgetattr failed: %s\n", strerror(errno));
41
		exit(1);
42
	}
43
	if (tcgetattr(ttyfd, &ttback) < 0)
44
		bipmkpw_fatal("tcgetattr failed", strerror(errno));
43 45

  
44 46
	memcpy(&tt, &ttback, sizeof(ttback));
47
// unsigned conversion from ‘int’ to ‘tcflag_t’ {aka ‘unsigned int’} changes value from ‘-11’ to ‘4294967285’
48
#pragma GCC diagnostic push
49
#pragma GCC diagnostic ignored "-Wsign-conversion"
45 50
	tt.c_lflag &= ~(ICANON|ECHO);
46
	if (tcsetattr(ttyfd, TCSANOW, &tt) < 0) {
47
		fprintf(stderr, "tcsetattr failed: %s\n", strerror(errno));
48
		exit(1);
49
	}
51
#pragma GCC diagnostic pop
52
	if (tcsetattr(ttyfd, TCSANOW, &tt) < 0)
53
		bipmkpw_fatal("tcsetattr failed", strerror(errno));
50 54

  
51
	write(ttyfd, "Password: ", 10);
55
	if (!write(ttyfd, "Password: ", (size_t)10))
56
		bipmkpw_fatal("tty write failed", strerror(errno));
52 57

  
53 58
	int idx = 0;
54 59
	int valid = 1;
55 60
	while (idx < buflen) {
56
		read(ttyfd, buffer+idx, 1);
61
		ssize_t rbytes = read(ttyfd, buffer+idx, (size_t)1);
62
		if (rbytes <= 0) {
63
			break;
64
		}
57 65
		if (buffer[idx] == '\n') {
58 66
			buffer[idx] = 0;
59 67
			break;
......
63 71
		idx++;
64 72
	}
65 73

  
66
	write(ttyfd, "\n", 1);
74
	if (!write(ttyfd, "\n", (size_t)1))
75
		bipmkpw_fatal("tty write failed", strerror(errno));
67 76

  
68 77
	tcsetattr(ttyfd, TCSANOW, &ttback);
69 78
	close(ttyfd);
......
84 93
	readpass(str, 256);
85 94
	str[255] = 0;
86 95

  
96
// passing argument 1 of ‘srand’ with different width due to prototype [-Werror=traditional-conversion]
97
// conversion from ‘time_t’ {aka ‘long int’} to ‘unsigned int’ may change value [-Werror=conversion]
98
// We don't care.
99
#pragma GCC diagnostic push
100
#pragma GCC diagnostic ignored "-Wtraditional-conversion"
101
#pragma GCC diagnostic ignored "-Wconversion"
87 102
	// the time used to type the pass is entropy
88 103
	srand(time(NULL));
89
	seed = rand();
104
#pragma GCC diagnostic pop
105
	seed = (unsigned)rand(); // rand should be > 0
90 106

  
91 107
	md5 = chash_double(str, seed);
92 108
        for (i = 0; i < 20; i++)
93
- 
(3-3/24)