Просмотр исходного кода

totemconfig.c: don't hard-code "128"...

* exec/totemconfig.c (read_keyfile): Don't hard-code "128".
Use sizeof(...) instead.
Avoid duplicate "close(fd)" calls.

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2039 fd59a12c-fef9-0310-b244-a6a79926bd2f
Jim Meyering 17 лет назад
Родитель
Сommit
66c168efee
1 измененных файлов с 10 добавлено и 7 удалено
  1. 10 7
      exec/totemconfig.c

+ 10 - 7
exec/totemconfig.c

@@ -603,6 +603,8 @@ static int read_keyfile (
 {
 {
 	int fd;
 	int fd;
 	int res;
 	int res;
+	ssize_t expected_key_len = sizeof (totem_config->private_key);
+	int saved_errno;
 
 
 	fd = open (key_location, O_RDONLY);
 	fd = open (key_location, O_RDONLY);
 	if (fd == -1) {
 	if (fd == -1) {
@@ -612,25 +614,26 @@ static int read_keyfile (
 		goto parse_error;
 		goto parse_error;
 	}
 	}
 
 
-	res = read (fd, totem_config->private_key, 128);
+	res = read (fd, totem_config->private_key, expected_key_len);
+	saved_errno = errno;
+	close (fd);
+
 	if (res == -1) {
 	if (res == -1) {
-		close (fd);
 		snprintf (error_string_response, sizeof(error_string_response),
 		snprintf (error_string_response, sizeof(error_string_response),
 			"Could not read %s: %s\n",
 			"Could not read %s: %s\n",
-			 key_location, strerror (errno));
+			 key_location, strerror (saved_errno));
 		goto parse_error;
 		goto parse_error;
 	}
 	}
 
 
-	totem_config->private_key_len = 128;
+	totem_config->private_key_len = expected_key_len;
 
 
-	if (res != 128) {
-		close (fd);
+	if (res != expected_key_len) {
 		snprintf (error_string_response, sizeof(error_string_response),
 		snprintf (error_string_response, sizeof(error_string_response),
 			"Could only read %d bits of 1024 bits from %s.\n",
 			"Could only read %d bits of 1024 bits from %s.\n",
 			 res * 8, key_location);
 			 res * 8, key_location);
 		goto parse_error;
 		goto parse_error;
 	}
 	}
-	close (fd);
+
 	return 0;
 	return 0;
 
 
 parse_error:
 parse_error: