basic: util - fix errorhandling in unhexmem()
We were ignoring failures from unhexchar, which meant that invalid hex characters were being turned into garbage rather than the string rejected. Fix this by making unhexmem return an error code, also change the API slightly, to return the size of the returned memory, reflecting the fact that the memory is a binary blob,and not a string. For convenience, still append a trailing NULL byte to the returned memory (not included in the returned size), allowing callers to treat it as a string without doing a second copy.
This commit is contained in:
+18
-6
@@ -916,30 +916,42 @@ char *hexmem(const void *p, size_t l) {
|
||||
return r;
|
||||
}
|
||||
|
||||
void *unhexmem(const char *p, size_t l) {
|
||||
uint8_t *r, *z;
|
||||
int unhexmem(const char *p, size_t l, void **mem, size_t *len) {
|
||||
_cleanup_free_ uint8_t *r = NULL;
|
||||
uint8_t *z;
|
||||
const char *x;
|
||||
|
||||
assert(mem);
|
||||
assert(len);
|
||||
assert(p);
|
||||
|
||||
z = r = malloc((l + 1) / 2 + 1);
|
||||
if (!r)
|
||||
return NULL;
|
||||
return -ENOMEM;
|
||||
|
||||
for (x = p; x < p + l; x += 2) {
|
||||
int a, b;
|
||||
|
||||
a = unhexchar(x[0]);
|
||||
if (x+1 < p + l)
|
||||
if (a < 0)
|
||||
return a;
|
||||
else if (x+1 < p + l) {
|
||||
b = unhexchar(x[1]);
|
||||
else
|
||||
if (b < 0)
|
||||
return b;
|
||||
} else
|
||||
b = 0;
|
||||
|
||||
*(z++) = (uint8_t) a << 4 | (uint8_t) b;
|
||||
}
|
||||
|
||||
*z = 0;
|
||||
return r;
|
||||
|
||||
*mem = r;
|
||||
r = NULL;
|
||||
*len = (l + 1) / 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char octchar(int x) {
|
||||
|
||||
+1
-1
@@ -614,7 +614,7 @@ static inline void *mempset(void *s, int c, size_t n) {
|
||||
}
|
||||
|
||||
char *hexmem(const void *p, size_t l);
|
||||
void *unhexmem(const char *p, size_t l);
|
||||
int unhexmem(const char *p, size_t l, void **mem, size_t *len);
|
||||
|
||||
char *strextend(char **x, ...) _sentinel_;
|
||||
char *strrep(const char *s, unsigned n);
|
||||
|
||||
Reference in New Issue
Block a user