/* Create, modify, and extract from archives.
- Copyright (C) 2005-2012, 2016 Red Hat, Inc.
+ Copyright (C) 2005-2012, 2016, 2017 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2005.
do_oper_extract (int oper, const char *arfname, char **argv, int argc,
long int instance)
{
- bool found[argc];
+ bool found[argc > 0 ? argc : 1];
memset (found, '\0', sizeof (found));
size_t name_max = 0;
goto nonew_unlink;
errout:
-#ifdef DEBUG
elf_end (elf);
arlib_fini ();
close (fd);
-#endif
not_found (argc, argv, found);
}
-static void
+/* Prints the given value in the given buffer without a trailing zero char.
+ Returns false if the given value doesn't fit in the given buffer. */
+static bool
no0print (bool ofmt, char *buf, int bufsize, long int val)
{
char tmpbuf[bufsize + 1];
- snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
+ int ret = snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld",
+ bufsize, val);
+ if (ret >= (int) sizeof (tmpbuf))
+ return false;
memcpy (buf, tmpbuf, bufsize);
+ return true;
}
const char *member)
{
int status = 0;
- Elf *elf;
+ Elf *elf = NULL;
struct stat st;
int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
if (status != 0)
{
-#ifdef DEBUG
elf_end (elf);
arlib_fini ();
close (fd);
-#endif
return status;
}
memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
}
- no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
- all->sec);
- no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid), all->uid);
- no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid), all->gid);
- no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
- all->mode);
- no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
- all->size);
+ if (! no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
+ all->sec))
+ {
+ error (0, errno, gettext ("cannot represent ar_date"));
+ goto nonew_unlink;
+ }
+ if (! no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid),
+ all->uid))
+ {
+ error (0, errno, gettext ("cannot represent ar_uid"));
+ goto nonew_unlink;
+ }
+ if (! no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid),
+ all->gid))
+ {
+ error (0, errno, gettext ("cannot represent ar_gid"));
+ goto nonew_unlink;
+ }
+ if (! no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
+ all->mode))
+ {
+ error (0, errno, gettext ("cannot represent ar_mode"));
+ goto nonew_unlink;
+ }
+ if (! no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
+ all->size))
+ {
+ error (0, errno, gettext ("cannot represent ar_size"));
+ goto nonew_unlink;
+ }
memcpy (arhdr.ar_fmag, ARFMAG, sizeof (arhdr.ar_fmag));
if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
goto nonew_unlink;
errout:
-#ifdef DEBUG
+ for (int cnt = 0; cnt < argc; ++cnt)
+ elf_end (found[cnt]->elf);
+
elf_end (elf);
arlib_fini ();
- close (fd);
-#endif
+ if (fd != -1)
+ close (fd);
return status;
}
--- /dev/null
+#! /bin/bash
+# Copyright (C) 2017 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/test-subr.sh
+
+tempfiles objects.list test.ar
+
+echo Make a sorted list of the just build src .o files.
+(cd ${abs_top_builddir}/src; ls *.o | sort) > objects.list
+cat objects.list
+
+echo Create a new ar file with the .o files.
+testrun ${abs_top_builddir}/src/ar -r test.ar \
+ $(echo ${abs_top_builddir}/src/*.o | sort)
+
+echo List the ar file contents.
+testrun_compare ${abs_top_builddir}/src/ar -t test.ar < objects.list
+
+echo Delete all objects again.
+testrun ${abs_top_builddir}/src/ar -d test.ar $(cat objects.list)
+
+echo Check new ar file is now empty
+testrun_compare ${abs_top_builddir}/src/ar -t test.ar << EOF
+EOF
+
+exit 0