am d1c7766a: am e9f6c480: am 3ec5d895: Merge "Fix utime/utimes when passed a NULL pointer."

* commit 'd1c7766aea5a4a6c864f6ad7ac097cf6bc2a587a':
  Fix utime/utimes when passed a NULL pointer.
This commit is contained in:
Elliott Hughes 2013-10-28 13:36:25 -07:00 committed by Android Git Automerger
commit aa97d508dc
3 changed files with 18 additions and 6 deletions

View File

@ -34,9 +34,13 @@
int utimes(const char* path, const timeval tv[2]) { int utimes(const char* path, const timeval tv[2]) {
timespec ts[2]; timespec ts[2];
if (!timespec_from_timeval(ts[0], tv[0]) || !timespec_from_timeval(ts[1], tv[1])) { timespec* ts_ptr = NULL;
errno = EINVAL; if (tv != NULL) {
return -1; if (!timespec_from_timeval(ts[0], tv[0]) || !timespec_from_timeval(ts[1], tv[1])) {
errno = EINVAL;
return -1;
}
ts_ptr = ts;
} }
return utimensat(AT_FDCWD, path, ts, 0); return utimensat(AT_FDCWD, path, ts_ptr, 0);
} }

View File

@ -25,6 +25,7 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#ifndef _UTIME_H_ #ifndef _UTIME_H_
#define _UTIME_H_ #define _UTIME_H_
@ -34,9 +35,8 @@
__BEGIN_DECLS __BEGIN_DECLS
extern int utime(const char *, const struct utimbuf *); extern int utime(const char*, const struct utimbuf*);
__END_DECLS __END_DECLS
#endif /* _UTIME_H_ */ #endif /* _UTIME_H_ */

View File

@ -19,6 +19,8 @@
#include <errno.h> #include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include "TemporaryFile.h"
TEST(sys_time, utimes) { TEST(sys_time, utimes) {
timeval tv[2]; timeval tv[2];
memset(&tv, 0, sizeof(tv)); memset(&tv, 0, sizeof(tv));
@ -38,3 +40,9 @@ TEST(sys_time, utimes) {
ASSERT_EQ(-1, utimes("/", tv)); ASSERT_EQ(-1, utimes("/", tv));
ASSERT_EQ(EINVAL, errno); ASSERT_EQ(EINVAL, errno);
} }
// http://b/11383777
TEST(sys_time, utimes_NULL) {
TemporaryFile tf;
ASSERT_EQ(0, utimes(tf.filename, NULL));
}