summaryrefslogtreecommitdiff
path: root/chrpath.c
diff options
context:
space:
mode:
Diffstat (limited to 'chrpath.c')
-rw-r--r--chrpath.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/chrpath.c b/chrpath.c
index 6dfcac1..207e369 100644
--- a/chrpath.c
+++ b/chrpath.c
@@ -89,6 +89,7 @@ chrpath(const char *filename, const char *newpath, int convert)
if (0 != elf_find_dynamic_section(fd, &ehdr, &phdr))
{
perror("found no dynamic section");
+ elf_close(fd);
return 1;
}
@@ -96,14 +97,16 @@ chrpath(const char *filename, const char *newpath, int convert)
if (dyns == NULL)
{
perror ("allocating memory for dynamic section");
+ elf_close(fd);
return 1;
}
memset(dyns, 0, PHDR(p_filesz));
if (lseek(fd, PHDR(p_offset), SEEK_SET) == -1
- || read(fd, dyns, PHDR(p_filesz)) != (int)PHDR(p_filesz))
+ || read(fd, dyns, PHDR(p_filesz)) != (ssize_t)PHDR(p_filesz))
{
perror ("reading dynamic section");
free(dyns);
+ elf_close(fd);
return 1;
}
@@ -121,6 +124,7 @@ chrpath(const char *filename, const char *newpath, int convert)
{
printf("%s: no rpath or runpath tag found.\n", filename);
free(dyns);
+ elf_close(fd);
return 2;
}
@@ -128,6 +132,7 @@ chrpath(const char *filename, const char *newpath, int convert)
{
perror ("positioning for sections");
free(dyns);
+ elf_close(fd);
return 1;
}
@@ -138,6 +143,7 @@ chrpath(const char *filename, const char *newpath, int convert)
{
perror ("reading section header");
free(dyns);
+ elf_close(fd);
return 1;
}
if (SHDR_W(sh_type) == SHT_STRTAB)
@@ -147,31 +153,36 @@ chrpath(const char *filename, const char *newpath, int convert)
{
fprintf (stderr, "No string table found.\n");
free(dyns);
+ elf_close(fd);
return 2;
}
- strtab = (char *)malloc(SHDR_O(sh_size));
+ /* +1 for forced trailing null */
+ strtab = (char *)calloc(1, SHDR_O(sh_size)+1);
if (strtab == NULL)
{
perror ("allocating memory for string table");
free(dyns);
+ elf_close(fd);
return 1;
}
- memset(strtab, 0, SHDR_O(sh_size));
if (lseek(fd, SHDR_O(sh_offset), SEEK_SET) == -1)
{
perror ("positioning for string table");
free(strtab);
free(dyns);
+ elf_close(fd);
return 1;
}
- if (read(fd, strtab, SHDR_O(sh_size)) != (int)SHDR_O(sh_size))
+ if (read(fd, strtab, SHDR_O(sh_size)) != (ssize_t)SHDR_O(sh_size))
{
perror ("reading string table");
free(strtab);
free(dyns);
+ elf_close(fd);
return 1;
}
+ strtab[SHDR_O(sh_size)] = 0; /* make sure printed string is null terminated */
if ((int)SHDR_O(sh_size) < rpathoff)
{
@@ -179,6 +190,7 @@ chrpath(const char *filename, const char *newpath, int convert)
elf_tagname(DYNSS(rpath_dyns_index, d_tag)));
free(strtab);
free(dyns);
+ elf_close(fd);
return 5;
}
rpath = strtab+rpathoff;
@@ -196,6 +208,9 @@ chrpath(const char *filename, const char *newpath, int convert)
|| write(fd, dyns, PHDR(p_filesz)) != (int)PHDR(p_filesz))
{
perror ("converting RPATH to RUNPATH");
+ free(strtab);
+ free(dyns);
+ elf_close(fd);
return 1;
}
printf("%s: RPATH converted to RUNPATH\n", filename);
@@ -209,6 +224,7 @@ chrpath(const char *filename, const char *newpath, int convert)
{
free(dyns);
free(strtab);
+ elf_close(fd);
return 0;
}
@@ -232,6 +248,7 @@ chrpath(const char *filename, const char *newpath, int convert)
newpath, rpathlen);
free(dyns);
free(strtab);
+ elf_close(fd);
return 7;
}
@@ -243,6 +260,7 @@ chrpath(const char *filename, const char *newpath, int convert)
perror ("positioning for RPATH");
free(dyns);
free(strtab);
+ elf_close(fd);
return 1;
}
if (write(fd, rpath, rpathlen) != (int)rpathlen)
@@ -250,6 +268,7 @@ chrpath(const char *filename, const char *newpath, int convert)
perror ("writing RPATH");
free(dyns);
free(strtab);
+ elf_close(fd);
return 1;
}
printf("%s: new %s: %s\n", filename,