1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-03 00:58:21 +03:00

Merge pull request #13022 from keszybz/coverity-cleanups

Coverity cleanups
This commit is contained in:
Lennart Poettering 2019-07-12 07:37:44 +02:00 committed by GitHub
commit 27dd6e1b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 50 deletions

View File

@ -1143,7 +1143,14 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
prefix2 = strjoina(prefix, "\t");
fprintf(f,
"%s-> Unit %s:\n"
"%s-> Unit %s:\n",
prefix, u->id);
SET_FOREACH(t, u->names, i)
if (!streq(t, u->id))
fprintf(f, "%s\tAlias: %s\n", prefix, t);
fprintf(f,
"%s\tDescription: %s\n"
"%s\tInstance: %s\n"
"%s\tUnit Load State: %s\n"
@ -1161,7 +1168,6 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
"%s\tSlice: %s\n"
"%s\tCGroup: %s\n"
"%s\tCGroup realized: %s\n",
prefix, u->id,
prefix, unit_description(u),
prefix, strna(u->instance),
prefix, unit_load_state_to_string(u->load_state),
@ -1213,9 +1219,6 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
fprintf(f, "%s\tCGroup delegate mask: %s\n", prefix, strnull(s));
}
SET_FOREACH(t, u->names, i)
fprintf(f, "%s\tName: %s\n", prefix, t);
if (!sd_id128_is_null(u->invocation_id))
fprintf(f, "%s\tInvocation ID: " SD_ID128_FORMAT_STR "\n",
prefix, SD_ID128_FORMAT_VAL(u->invocation_id));

View File

@ -178,37 +178,37 @@ static int ask_password_keyring(const char *keyname, AskPasswordFlags flags, cha
return retrieve_key(serial, ret);
}
static void backspace_chars(int ttyfd, size_t p) {
static int backspace_chars(int ttyfd, size_t p) {
if (ttyfd < 0)
return;
return 0;
while (p > 0) {
p--;
_cleanup_free_ char *buf = malloc_multiply(3, p);
if (!buf)
return log_oom();
loop_write(ttyfd, "\b \b", 3, false);
}
for (size_t i = 0; i < p; i++)
memcpy(buf + 3 * i, "\b \b", 3);
return loop_write(ttyfd, buf, 3*p, false);
}
static void backspace_string(int ttyfd, const char *str) {
size_t m;
static int backspace_string(int ttyfd, const char *str) {
assert(str);
if (ttyfd < 0)
return;
/* Backspaces through enough characters to entirely undo printing of the specified string. */
m = utf8_n_codepoints(str);
if (m == (size_t) -1)
m = strlen(str); /* Not a valid UTF-8 string? If so, let's backspace the number of bytes output. Most
* likely this happened because we are not in an UTF-8 locale, and in that case that
* is the correct thing to do. And even if it's not, terminals tend to stop
* backspacing at the leftmost column, hence backspacing too much should be mostly
* OK. */
if (ttyfd < 0)
return 0;
backspace_chars(ttyfd, m);
size_t m = utf8_n_codepoints(str);
if (m == (size_t) -1)
m = strlen(str); /* Not a valid UTF-8 string? If so, let's backspace the number of bytes
* output. Most likely this happened because we are not in an UTF-8 locale,
* and in that case that is the correct thing to do. And even if it's not,
* terminals tend to stop backspacing at the leftmost column, hence
* backspacing too much should be mostly OK. */
return backspace_chars(ttyfd, m);
}
int ask_password_tty(
@ -374,7 +374,7 @@ int ask_password_tty(
if (c == 21) { /* C-u */
if (!(flags & ASK_PASSWORD_SILENT))
backspace_string(ttyfd, passphrase);
(void) backspace_string(ttyfd, passphrase);
explicit_bzero_safe(passphrase, sizeof(passphrase));
p = codepoint = 0;
@ -385,7 +385,7 @@ int ask_password_tty(
size_t q;
if (!(flags & ASK_PASSWORD_SILENT))
backspace_chars(ttyfd, 1);
(void) backspace_chars(ttyfd, 1);
/* Remove a full UTF-8 codepoint from the end. For that, figure out where the
* last one begins */
@ -423,7 +423,7 @@ int ask_password_tty(
} else if (c == '\t' && !(flags & ASK_PASSWORD_SILENT)) {
backspace_string(ttyfd, passphrase);
(void) backspace_string(ttyfd, passphrase);
flags |= ASK_PASSWORD_SILENT;
/* ... or by pressing TAB at any time. */

View File

@ -139,7 +139,7 @@ static void test_auto_erase_memory(void) {
assert_se(p1 = new(uint8_t, 1024));
assert_se(p2 = new(uint8_t, 1024));
genuine_random_bytes(p1, 1024, RANDOM_BLOCK);
assert_se(genuine_random_bytes(p1, 1024, RANDOM_BLOCK) == 0);
/* before we exit the scope, do something with this data, so that the compiler won't optimize this away */
memcpy(p2, p1, 1024);

View File

@ -52,8 +52,8 @@ static void test_copy_file_fd(void) {
char in_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
char out_fn[] = "/tmp/test-copy-file-fd-XXXXXX";
_cleanup_close_ int in_fd = -1, out_fd = -1;
char text[] = "boohoo\nfoo\n\tbar\n";
char buf[64] = {0};
const char *text = "boohoo\nfoo\n\tbar\n";
char buf[64] = {};
log_info("%s", __func__);
@ -67,7 +67,7 @@ static void test_copy_file_fd(void) {
assert_se(copy_file_fd(in_fn, out_fd, COPY_REFLINK) >= 0);
assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
assert_se(read(out_fd, buf, sizeof buf) == (ssize_t) strlen(text));
assert_se(streq(buf, text));
unlink(in_fn);

View File

@ -427,7 +427,7 @@ static void test_write_string_file(void) {
static void test_write_string_file_no_create(void) {
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX";
_cleanup_close_ int fd;
char buf[64] = {0};
char buf[64] = {};
fd = mkostemp_safe(fn);
assert_se(fd >= 0);
@ -435,7 +435,7 @@ static void test_write_string_file_no_create(void) {
assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
assert_se(write_string_file(fn, "boohoo", 0) == 0);
assert_se(read(fd, buf, sizeof(buf)) == STRLEN("boohoo\n"));
assert_se(read(fd, buf, sizeof buf) == (ssize_t) strlen("boohoo\n"));
assert_se(streq(buf, "boohoo\n"));
}

View File

@ -524,14 +524,14 @@ static void test_getpid_measure(void) {
(void) getpid();
q = now(CLOCK_MONOTONIC) - t;
log_info(" glibc getpid(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
log_info(" glibc getpid(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
t = now(CLOCK_MONOTONIC);
for (i = 0; i < MEASURE_ITERATIONS; i++)
(void) getpid_cached();
q = now(CLOCK_MONOTONIC) - t;
log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
log_info("getpid_cached(): %lf µs each\n", (double) q / MEASURE_ITERATIONS);
}
static void test_safe_fork(void) {

View File

@ -1107,9 +1107,20 @@ static int on_ctrl_msg(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, co
return 1;
}
static int synthesize_change_one(sd_device *dev, const char *syspath) {
const char *filename;
int r;
filename = strjoina(syspath, "/uevent");
log_device_debug(dev, "device is closed, synthesising 'change' on %s", syspath);
r = write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
if (r < 0)
return log_device_debug_errno(dev, r, "Failed to write 'change' to %s: %m", filename);
return 0;
}
static int synthesize_change(sd_device *dev) {
const char *subsystem, *sysname, *devname, *syspath, *devtype;
char filename[PATH_MAX];
int r;
r = sd_device_get_subsystem(dev, &subsystem);
@ -1197,9 +1208,7 @@ static int synthesize_change(sd_device *dev) {
* We have partitions but re-reading the partition table did not
* work, synthesize "change" for the disk and all partitions.
*/
log_debug("Device '%s' is closed, synthesising 'change'", devname);
strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
(void) synthesize_change_one(dev, syspath);
FOREACH_DEVICE(e, d) {
const char *t, *n, *s;
@ -1212,17 +1221,11 @@ static int synthesize_change(sd_device *dev) {
sd_device_get_syspath(d, &s) < 0)
continue;
log_debug("Device '%s' is closed, synthesising partition '%s' 'change'", devname, n);
strscpyl(filename, sizeof(filename), s, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
(void) synthesize_change_one(dev, s);
}
return 0;
}
log_debug("Device %s is closed, synthesising 'change'", devname);
strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
} else
(void) synthesize_change_one(dev, syspath);
return 0;
}