Skip to content

Commit d042080

Browse files
committed
Log errors of level CRIT around memory allocations
This is aligned with the "Module Wrtiers' Guide" of pam-linux. Enable mock_syslog for tests/expand in Autoconf, since tests/expand will now use syslog(3). Not needed on CMake.
1 parent 02e7261 commit d042080

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

expand.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
* Copyright (C) 2023 Yubico AB - See COPYING
33
*/
44

5+
#include <errno.h>
6+
#include <limits.h>
57
#include <stdint.h>
68
#include <stdlib.h>
79
#include <string.h>
8-
#include <limits.h>
910

1011
#include "util.h"
12+
#include "logging.h"
1113

1214
static int buf_write(uint8_t **dst, size_t *size, const void *src, size_t n) {
1315
if (*size < n) {
@@ -42,7 +44,11 @@ char *expand_variables(const char *str, const char *user) {
4244
size_t size = PATH_MAX;
4345
int ok = -1;
4446

45-
if (str == NULL || (tail = head = malloc(size)) == NULL) {
47+
if (str == NULL)
48+
return NULL;
49+
50+
if ((tail = head = malloc(size)) == NULL) {
51+
LOG(LOG_CRIT, "Unable to allocate memory: %s", strerror(errno));
4652
return NULL;
4753
}
4854

pam-u2f.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ static char *resolve_authfile_path(const cfg_t *cfg, const struct passwd *user,
7373
path = cfg->auth_file;
7474
}
7575

76-
if (dir == NULL || *dir != '/' || path == NULL ||
77-
asprintf(&authfile, "%s/%s", dir, path) == -1)
78-
authfile = NULL;
76+
if (dir == NULL || *dir != '/' || path == NULL)
77+
return NULL;
78+
79+
if (asprintf(&authfile, "%s/%s", dir, path) == -1) {
80+
LOG(LOG_CRIT, "Unable to allocate memory: %s", strerror(errno));
81+
return NULL;
82+
}
7983

8084
return authfile;
8185
}

tests/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ get_devices_SOURCES = get_devices.c mock_syslog.c
1616
get_devices_LDADD = $(top_builddir)/libmodule.la
1717

1818
check_PROGRAMS += expand
19+
expand_SOURCES = expand.c mock_syslog.c
1920
expand_LDADD = $(top_builddir)/libmodule.la
2021

2122
check_PROGRAMS += cfg

0 commit comments

Comments
 (0)