mercurial/bitmanipulation.h
author Jun Wu <quark@fb.com>
Tue, 30 Jan 2018 20:32:48 -0800
branchstable
changeset 35810 113a30b87716
parent 34697 ce77b0563228
child 38303 1fb2510cf8c8
permissions -rw-r--r--
lazymanifest: avoid reading uninitialized memory I got errors running tests with clang UBSAN [1] enabled. One of them is: ``` --- test-dirstate.t +++ test-dirstate.t.err @@ -85,9 +85,115 @@ $ echo "[extensions]" >> .hg/hgrc $ echo "dirstateex=../dirstateexception.py" >> .hg/hgrc $ hg up 0 - abort: simulated error while recording dirstateupdates - [255] + mercurial/cext/manifest.c:781:13: runtime error: load of value 190, which is not a valid value for type 'bool' + #0 0x7f668a8cf748 in lazymanifest_diff mercurial/cext/manifest.c:781 + #1 0x7f6692fc1dc4 in call_function Python-2.7.11/Python/ceval.c:4350 + ....... + SUMMARY: UndefinedBehaviorSanitizer: invalid-bool-load mercurial/cext/manifest.c:781:13 in + [1] $ hg log -r . -T '{rev}\n' 1 $ hg status - ? a ``` While the code is not technically wrong, but switching the condition would make clang UBSAN happy. So let's do it. The uninitialized memory could come from, for example, `lazymanifest_copy` allocates `self->maxlines` items but only writes the first `self->lines` items. [1]: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html Test Plan: Run `test-dirstate.t` with UBSAN and it no longer reports the issue. Differential Revision: https://phab.mercurial-scm.org/D1948

#ifndef _HG_BITMANIPULATION_H_
#define _HG_BITMANIPULATION_H_

#include <string.h>

#include "compat.h"

static inline uint32_t getbe32(const char *c)
{
	const unsigned char *d = (const unsigned char *)c;

	return ((d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]));
}

static inline int16_t getbeint16(const char *c)
{
	const unsigned char *d = (const unsigned char *)c;

	return ((d[0] << 8) | (d[1]));
}

static inline uint16_t getbeuint16(const char *c)
{
	const unsigned char *d = (const unsigned char *)c;

	return ((d[0] << 8) | (d[1]));
}

static inline void putbe32(uint32_t x, char *c)
{
	c[0] = (x >> 24) & 0xff;
	c[1] = (x >> 16) & 0xff;
	c[2] = (x >> 8) & 0xff;
	c[3] = (x)&0xff;
}

static inline double getbefloat64(const char *c)
{
	const unsigned char *d = (const unsigned char *)c;
	double ret;
	int i;
	uint64_t t = 0;
	for (i = 0; i < 8; i++) {
		t = (t << 8) + d[i];
	}
	memcpy(&ret, &t, sizeof(t));
	return ret;
}

#endif