Commit b3a7d678 authored by Colin Watson's avatar Colin Watson

Handle asprintf failures consistently.

parent 788b007d
......@@ -4,26 +4,25 @@ ifdef DEBUG
CFLAGS += -g3
endif
LIBS = -lparted
STRIP = strip
PARTCONF_OBJS = util.o partconf.o find-parts.o
PARTCONF_OBJS = xasprintf.o util.o partconf.o find-parts.o
all: partconf find-partitions mkfstab mountpoint
partconf: LDLIBS := -ldl -ldebconfclient -ldebian-installer -lparted
partconf: $(PARTCONF_OBJS)
$(CC) $(CFLAGS) -o $@ $^ -ldl -ldebconfclient -ldebian-installer -lparted
find-partitions: partconf.h find-parts.c util.o
$(CC) $(CFLAGS) -o $@ -DFIND_PARTS_MAIN find-parts.c util.o -lparted
find-partitions: partconf.h find-parts.c util.o xasprintf.o
$(CC) $(CFLAGS) -o $@ -DFIND_PARTS_MAIN find-parts.c util.o xasprintf.o -lparted
mkfstab: mkfstab.c mkfstab.h
$(CC) $(CFLAGS) -o $@ mkfstab.c -ldebian-installer
mkfstab: LDLIBS := -ldebian-installer
mkfstab: mkfstab.o xasprintf.o
mountpoint: mountpoint.c
$(CC) $(CFLAGS) -o $@ mountpoint.c -ldebian-installer
mountpoint: LDLIBS := -ldebian-installer
mountpoint: mountpoint.o
xasprintf.o: xasprintf.c xasprintf.h
util.o: util.c
partconf.o: partconf.c partconf.h
find-parts.o: find-parts.c partconf.h
......@@ -36,10 +35,10 @@ small: clean partconf find-partitions mkfstab mountpoint
$(STRIP) --remove-section=.comment --remove-section=.note mountpoint
test-mkfstab: mkfstab.c mkfstab.h
$(CC) $(CFLAGS) -o test-mkfstab -DTEST mkfstab.c $(LIBS) -ldebian-installer
$(CC) $(CFLAGS) -o test-mkfstab -DTEST mkfstab.c xasprintf.o -lparted -ldebian-installer
test-partconf: find-parts.o partconf.c partconf.h util.o
$(CC) $(CFLAGS) -o test-partconf -DTEST partconf.c find-parts.o util.o $(LIBS) -ldebian-installer
$(CC) $(CFLAGS) -o test-partconf -DTEST partconf.c find-parts.o util.o xasprintf.o -lparted -ldebian-installer
check: test-mkfstab test-partconf
./test-mkfstab
......
partconf (1.42) UNRELEASED; urgency=low
[ Dmitrijs Ledkovs ]
* Set debian source format to '3.0 (native)'.
* Bump debhelper compat level to 9.
* Set Vcs-* to canonical format.
[ Colin Watson ]
* Handle asprintf failures consistently.
-- Dmitrijs Ledkovs <xnox@debian.org> Fri, 07 Jun 2013 00:26:08 +0100
partconf (1.41) unstable; urgency=low
......
......@@ -14,6 +14,7 @@
#include <getopt.h>
#endif
#include "xasprintf.h"
#include "partconf.h"
// If it's an LVM volume, it's on the form
......@@ -35,7 +36,7 @@ test_lvm(struct partition *p)
*vol++ = '\0';
if (strchr(vol, '/') != NULL)
return;
asprintf(&procfile, "/proc/lvm/VGs/%s/LVs/%s", grp, vol);
procfile = xasprintf("/proc/lvm/VGs/%s/LVs/%s", grp, vol);
if ((fp = fopen(procfile, "r")) == NULL)
return;
while (fgets(buf, sizeof(buf), fp) != NULL) {
......@@ -106,7 +107,7 @@ test_raid(struct partition *p)
sscanf(buf, "%lld", &blocks);
p->size = blocks * 512L;
free(p->description);
asprintf(&p->description, "RAID logical volume %d", volno);
p->description = xasprintf("RAID logical volume %d", volno);
break;
}
}
......@@ -185,7 +186,7 @@ block_partition(const char *part)
if(entry->d_name[0] == '.')
continue;
asprintf(&cmd, "/bin/sh %s/%s \"%s\" 1>/dev/null 2>&1",
cmd = xasprintf("/bin/sh %s/%s \"%s\" 1>/dev/null 2>&1",
BLOCK_D, entry->d_name, part);
ret = system(cmd);
if(ret != 0) {
......@@ -268,7 +269,7 @@ get_all_partitions(struct partition *parts[], const int max_parts, bool ignore_f
if (sscanf(p->path, "/dev/hd%c%d", &drive, &part) == 2
&& drive >= 'a' && drive <= 'z')
asprintf(&p->description, "IDE%d %s\\, part. %d",
p->description = xasprintf("IDE%d %s\\, part. %d",
(drive - 'a') / 2 + 1, targets[(drive - 'a') % 2],
part);
else
......@@ -303,7 +304,7 @@ get_all_partitions(struct partition *parts[], const int max_parts, bool ignore_f
const char *bus_id = basename(buf);
if (sscanf(bus_id, "%d:%d:%d:%d",
&host, &bus, &target, &lun) == 4) {
asprintf(&p->description, "SCSI%d (%d\\,%d\\,%d) part. %d",
p->description = xasprintf("SCSI%d (%d\\,%d\\,%d) part. %d",
host + 1, bus, target, lun, part);
done = 1;
}
......
#include <errno.h>
#include "xasprintf.h"
#include "mkfstab.h"
#include "errno.h"
static int has_device(struct fstab_entry *entry) {
return (strcmp(entry->typ, "proc") != 0
......@@ -159,7 +161,7 @@ void get_fstab_d_dir() {
continue;
/* skipping directories */
asprintf(&fullname, "%s/%s", FSTAB_D, dentry->d_name);
fullname = xasprintf("%s/%s", FSTAB_D, dentry->d_name);
if(stat(fullname, &sbuf) == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno), fullname);
continue;
......@@ -235,7 +237,7 @@ void get_swapspaces() {
continue;
sscanf(line, "%s %*s %*s %*s %*s", filesystem);
asprintf(&swline, "%s\tnone\tswap\tsw", filesystem);
swline = xasprintf("%s\tnone\tswap\tsw", filesystem);
insert_line(swline);
}
......
......@@ -15,6 +15,7 @@
#include <cdebconf/debconfclient.h>
#include <debian-installer.h>
#include "xasprintf.h"
#include "partconf.h"
static struct debconfclient *debconf = NULL;
......@@ -64,7 +65,7 @@ build_part_choices(struct partition *parts[], const int part_count)
}
// pad with spaces
for (i = 0; i < part_count; i++) {
asprintf(&list[i], "%-*s", (int)max_len, parts[i]->description);
list[i] = xasprintf("%-*s", (int)max_len, parts[i]->description);
}
max_len = strlen("n/a");
for (i = 0; i < part_count; i++) {
......@@ -73,7 +74,7 @@ build_part_choices(struct partition *parts[], const int part_count)
}
// add and pad
for (i = 0; i < part_count; i++) {
asprintf(&tmp, "%s %-*s", list[i], (int)max_len,
tmp = xasprintf("%s %-*s", list[i], (int)max_len,
(parts[i]->size > 0) ? size_desc(parts[i]->size) : "n/a");
free(list[i]);
list[i] = tmp;
......@@ -87,7 +88,7 @@ build_part_choices(struct partition *parts[], const int part_count)
max_len = strlen(parts[i]->fstype);
}
for (i = 0; i < part_count; i++) {
asprintf(&tmp, "%s %-*s", list[i], (int)max_len,
tmp = xasprintf("%s %-*s", list[i], (int)max_len,
(parts[i]->op.filesystem != NULL) ? parts[i]->op.filesystem :
(parts[i]->fstype != NULL) ? parts[i]->fstype : "n/a");
free(list[i]);
......@@ -100,7 +101,7 @@ build_part_choices(struct partition *parts[], const int part_count)
}
if (max_len > 0)
for (i = 0; i < part_count; i++) {
asprintf(&tmp, "%s %-*s", list[i], (int)max_len,
tmp = xasprintf("%s %-*s", list[i], (int)max_len,
(parts[i]->op.mountpoint != NULL) ? parts[i]->op.mountpoint : "");
free(list[i]);
list[i] = tmp;
......@@ -110,7 +111,7 @@ build_part_choices(struct partition *parts[], const int part_count)
//printf("<%s>\n", list[0]);
for (i = 1; i < part_count; i++) {
//printf("<%s>\n", list[i]);
asprintf(&tmp2, "%s, %s", tmp, list[i]);
tmp2 = xasprintf("%s, %s", tmp, list[i]);
free(list[i]);
free(tmp);
tmp = tmp2;
......@@ -129,7 +130,7 @@ fs_to_choice(char *fs)
debconf_metaget(debconf, "partconf/internal-create-fs-choice", "description");
choicefmt = strdup(debconf->value);
}
asprintf(&tmp, choicefmt, fs);
tmp = xasprintf(choicefmt, fs);
return tmp;
}
......@@ -144,7 +145,7 @@ build_fs_choices(void)
if (tmp == NULL)
tmp2 = fs_to_choice(filesystems[i]);
else
asprintf(&tmp2, "%s, %s", tmp, fs_to_choice(filesystems[i]));
tmp2 = xasprintf("%s, %s", tmp, fs_to_choice(filesystems[i]));
free(tmp);
tmp = tmp2;
}
......@@ -356,7 +357,7 @@ finish(void)
// Create the file system/swap
if (strcmp(fs, "swap") == 0) {
append_message("partconf: Creating swap on %s\n", parts[i]->path);
asprintf(&cmd, "mkswap %s >/dev/null 2>>/var/log/messages", parts[i]->path);
cmd = xasprintf("mkswap %s >/dev/null 2>>/var/log/messages", parts[i]->path);
ret = system(cmd);
free(cmd);
if (ret != 0) {
......@@ -376,7 +377,7 @@ finish(void)
mkfs_opts="-f";
}
append_message("partconf: Creating %s file system on %s\n", fs, parts[i]->path);
asprintf(&cmd, "mkfs.%s %s %s >/dev/null 2>>/var/log/messages", fs, mkfs_opts, parts[i]->path);
cmd = xasprintf("mkfs.%s %s %s >/dev/null 2>>/var/log/messages", fs, mkfs_opts, parts[i]->path);
ret = system(cmd);
free(cmd);
if (ret != 0) {
......@@ -390,7 +391,7 @@ finish(void)
if (strcmp(fs, "swap") == 0 && !check_proc_swaps(parts[i]->path)) {
// Activate swap
append_message("partconf: Activating swap on %s\n", parts[i]->path);
asprintf(&cmd, "swapon %s >/dev/null 2>>/var/log/messages", parts[i]->path);
cmd = xasprintf("swapon %s >/dev/null 2>>/var/log/messages", parts[i]->path);
ret = system(cmd);
free(cmd);
/*
......@@ -405,7 +406,7 @@ finish(void)
// And mount
append_message("partconf: Mounting %s on %s\n",
parts[i]->path, parts[i]->op.mountpoint);
asprintf(&mntpt, "/target%s", parts[i]->op.mountpoint);
mntpt = xasprintf("/target%s", parts[i]->op.mountpoint);
makedirs(mntpt);
fs = parts[i]->op.filesystem ? parts[i]->op.filesystem : parts[i]->fstype;
ret = mount(parts[i]->path, mntpt, fs, 0xC0ED0000, NULL);
......
......@@ -5,6 +5,8 @@
#include <stdarg.h>
#include <sys/mount.h>
#include "xasprintf.h"
char *
size_desc(long long bytes)
{
......@@ -46,7 +48,7 @@ modprobe(const char *mod)
fputs("0\n", fp);
fclose(fp);
}
asprintf(&cmd, "modprobe %s >>/var/log/messages 2>&1", mod);
cmd = xasprintf("modprobe %s >>/var/log/messages 2>&1", mod);
system(cmd);
free(cmd);
if ((fp = fopen("/proc/sys/kernel/printk", "w")) != NULL) {
......@@ -67,7 +69,7 @@ check_proc_mounts(const char *mntpoint)
if ((fp = fopen("/proc/mounts", "r")) == NULL)
return 0;
asprintf(&tmp, "/target%s", mntpoint);
tmp = xasprintf("/target%s", mntpoint);
while (fgets(buf, sizeof(buf), fp) != NULL) {
sscanf(buf, "%*s %s", mnt);
if (strcmp(tmp, mnt) == 0) {
......
#define _GNU_SOURCE
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
char *
xasprintf(const char *format, ...)
{
va_list args;
char *result;
va_start(args, format);
if (vasprintf(&result, format, args) < 0) {
if (errno == ENOMEM) {
fputs("Out of memory!\n", stderr);
abort();
}
return NULL;
}
return result;
}
#ifndef XASPRINTF_H_
#define XASPRINTF_H_ 1
char *xasprintf(const char *format, ...);
#endif /* XASPRINTF_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment