Skip to content

Commit

Permalink
Add several more NULL checks
Browse files Browse the repository at this point in the history
  • Loading branch information
roehling committed Aug 23, 2023
1 parent 0d8df2e commit 3aac97f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ static int validate_uint(cfg_t* cfg, cfg_opt_t* opt)
static bool is_valid_domain_name(const char* s)
{
char prev = 0;
if (!s)
if (s == NULL)
return false;
if (!*s)
if (*s == 0)
return false;
while (*s)
while (*s != 0)
{
if (*s == '.' && prev == '.')
return false;
Expand Down
18 changes: 9 additions & 9 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static char* db_redis_read(database_t* db, const char* key)
snprintf(buffer, sizeof(buffer), "PostSRSd/%s", key);
redisContext* handle = (redisContext*)db->handle;
redisReply* reply = redisCommand(handle, "GET %s", buffer);
if (!reply)
if (reply == NULL)
{
log_warn("redis connection failure: %s", handle->errstr);
return NULL;
Expand All @@ -210,7 +210,7 @@ static bool db_redis_write(database_t* db, const char* key, const char* value,
bool success = true;
redisReply* reply =
redisCommand(handle, "SETEX %s %u %s", buffer, lifetime, value);
if (!reply)
if (reply == NULL)
{
log_warn("redis connection failure: %s", handle->errstr);
return false;
Expand All @@ -236,7 +236,7 @@ static bool db_redis_connect(database_t* db, const char* hostname, int port)
if (port > 0)
{
handle = redisConnect(hostname, port);
if (!handle)
if (handle == NULL)
goto alloc_fail;
if (handle->err)
goto conn_fail;
Expand All @@ -245,7 +245,7 @@ static bool db_redis_connect(database_t* db, const char* hostname, int port)
else
{
handle = redisConnectUnix(hostname);
if (!handle)
if (handle == NULL)
goto alloc_fail;
}
if (handle->err)
Expand All @@ -271,7 +271,7 @@ static bool db_redis_connect(database_t* db, const char* hostname, int port)
database_t* database_connect(const char* uri, bool create_if_not_exist)
{
MAYBE_UNUSED(create_if_not_exist);
if (!uri || !*uri)
if (uri == NULL || *uri == 0)
{
log_error("not database uri configured");
return NULL;
Expand Down Expand Up @@ -328,28 +328,28 @@ database_t* database_connect(const char* uri, bool create_if_not_exist)

char* database_read(database_t* db, const char* key)
{
if (db && key)
if (db != NULL && key != NULL)
return db->read(db, key);
return NULL;
}

bool database_write(database_t* db, const char* key, const char* value,
unsigned lifetime)
{
if (db && key && value)
if (db != NULL && key != NULL && value != NULL)
return db->write(db, key, value, lifetime);
return false;
}

void database_expire(database_t* db)
{
if (db && db->expire)
if (db != NULL && db->expire != NULL)
db->expire(db);
}

void database_disconnect(database_t* db)
{
if (db)
if (db != NULL)
db->disconnect(db);
free(db);
}
24 changes: 12 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static bool prepare_database(cfg_t* cfg)
{
database_t* db =
database_connect(cfg_getstr(cfg, "envelope-database"), true);
if (!db)
if (db == NULL)
return false;
database_expire(db);
database_disconnect(db);
Expand Down Expand Up @@ -188,7 +188,7 @@ static void handle_socketmap_client(cfg_t* cfg, srs_t* srs,
if (cfg_getint(cfg, "original-envelope") == SRS_ENVELOPE_DATABASE)
{
db = database_connect(cfg_getstr(cfg, "envelope-database"), false);
if (!db)
if (db == NULL)
return;
}
signal(SIGALRM, on_sigalrm);
Expand All @@ -204,7 +204,7 @@ static void handle_socketmap_client(cfg_t* cfg, srs_t* srs,
char* request = netstring_read(fp_read, buffer, sizeof(buffer), &len);
if (timeout)
break;
if (!request)
if (request == NULL)
{
if (!feof(fp_read) && !ferror(fp_read))
{
Expand All @@ -216,7 +216,7 @@ static void handle_socketmap_client(cfg_t* cfg, srs_t* srs,
}
alarm(0);
char* query_type = strtok_r(request, " ", &addr);
if (!query_type)
if (query_type == NULL)
{
netstring_write(fp_write, "PERM Invalid query.", 19);
fflush(fp_write);
Expand Down Expand Up @@ -291,14 +291,14 @@ int main(int argc, char** argv)
close(fd);
#endif
cfg = config_from_commandline(argc, argv);
if (!cfg)
if (cfg == NULL)
goto shutdown;
if (cfg_getbool(cfg, "syslog"))
log_enable_syslog();
if (cfg_getbool(cfg, "debug"))
log_set_verbosity(LogDebug);
srs = srs_from_config(cfg);
if (!srs)
if (srs == NULL)
goto shutdown;
if (!srs_domains_from_config(cfg, &srs_domain, &local_domains))
goto shutdown;
Expand All @@ -324,7 +324,7 @@ int main(int argc, char** argv)
if (pid_file && *pid_file)
{
pf = fopen(pid_file, "w");
if (!pf)
if (pf == NULL)
{
log_error("cannot open %s for writing", pid_file);
goto shutdown;
Expand All @@ -336,7 +336,7 @@ int main(int argc, char** argv)
goto shutdown;
if (!daemonize(cfg))
goto shutdown;
if (pf)
if (pf != NULL)
{
fprintf(pf, "%d", (int)getpid());
fclose(pf);
Expand Down Expand Up @@ -413,14 +413,14 @@ int main(int argc, char** argv)
for (unsigned i = 0; i < sizeof(socketmaps) / sizeof(int); ++i)
if (socketmaps[i] >= 0)
close(socketmaps[i]);
if (pf)
if (pf != NULL)
fclose(pf);
free(srs_domain);
if (local_domains)
if (local_domains != NULL)
domain_set_destroy(local_domains);
if (srs)
if (srs != NULL)
srs_free(srs);
if (cfg)
if (cfg != NULL)
cfg_free(cfg);
if (milter_pid > 0)
{
Expand Down
16 changes: 8 additions & 8 deletions src/milter.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ static sfsistat on_connect(SMFICTX* ctx, char* hostname, _SOCK_ADDR* hostaddr)
MAYBE_UNUSED(hostname);
MAYBE_UNUSED(hostaddr);
privdata_t* priv = malloc(sizeof(privdata_t));
if (!priv)
if (priv == NULL)
return SMFIS_TEMPFAIL;
priv->envfrom = NULL;
priv->envrcpt = list_create();
if (!priv->envrcpt)
if (priv->envrcpt == NULL)
return SMFIS_TEMPFAIL;
smfi_setpriv(ctx, priv);
return SMFIS_CONTINUE;
Expand All @@ -81,10 +81,10 @@ static sfsistat on_connect(SMFICTX* ctx, char* hostname, _SOCK_ADDR* hostaddr)
static sfsistat on_envfrom(SMFICTX* ctx, char** argv)
{
privdata_t* priv = smfi_getpriv(ctx);
if (!priv)
if (priv == NULL)
return SMFIS_TEMPFAIL;
char* from = strip_brackets(argv[0]);
if (!from)
if (from == NULL)
return SMFIS_TEMPFAIL;
priv->envfrom = from;
return SMFIS_CONTINUE;
Expand All @@ -93,10 +93,10 @@ static sfsistat on_envfrom(SMFICTX* ctx, char** argv)
static sfsistat on_envrcpt(SMFICTX* ctx, char** argv)
{
privdata_t* priv = smfi_getpriv(ctx);
if (!priv)
if (priv == NULL)
return SMFIS_TEMPFAIL;
char* rcpt = strip_brackets(argv[0]);
if (!rcpt)
if (rcpt == NULL)
return SMFIS_TEMPFAIL;
if (!list_append(priv->envrcpt, rcpt))
{
Expand All @@ -109,13 +109,13 @@ static sfsistat on_envrcpt(SMFICTX* ctx, char** argv)
static sfsistat on_eom(SMFICTX* ctx)
{
privdata_t* priv = smfi_getpriv(ctx);
if (!priv)
if (priv == NULL)
return SMFIS_TEMPFAIL;
database_t* db = NULL;
if (cfg_getint(g_cfg, "original-envelope") == SRS_ENVELOPE_DATABASE)
{
db = database_connect(cfg_getstr(g_cfg, "envelope-database"), false);
if (!db)
if (db == NULL)
return SMFIS_TEMPFAIL;
}
size_t rcpt_size = list_size(priv->envrcpt);
Expand Down
38 changes: 19 additions & 19 deletions src/srs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ char* postsrsd_forward(const char* addr, const char* domain, srs_t* srs,
const char** info)
{
const char* at = strchr(addr, '@');
if (error)
if (error != NULL)
*error = false;
if (info)
if (info != NULL)
*info = NULL;
if (!at)
if (at == NULL)
{
if (info)
if (info != NULL)
*info = "No domain.";
log_debug("<%s> not rewritten: no domain", addr);
return NULL;
}
const char* input_domain = at + 1;
if (domain_set_contains(local_domains, input_domain))
{
if (info)
if (info != NULL)
*info = "Need not rewrite local domain.";
log_debug("<%s> not rewritten: local domain", addr);
return NULL;
}
char db_alias_buf[35];
char* db_alias;
const char* sender = addr;
if (db && !SRS_IS_SRS_ADDRESS(addr))
if (db != NULL && !SRS_IS_SRS_ADDRESS(addr))
{
char digest[20];
sha_digest(digest, addr, strlen(addr));
db_alias = b32h_encode(digest, 20, db_alias_buf, sizeof(db_alias_buf));
if (!db_alias)
if (db_alias == NULL)
{
log_warn("<%s> not rewritten: aliasing error", addr);
if (error)
Expand All @@ -67,9 +67,9 @@ char* postsrsd_forward(const char* addr, const char* domain, srs_t* srs,
if (!database_write(db, db_alias, addr, srs->maxage * 86400))
{
log_warn("<%s> not rewritten: database error", addr);
if (error)
if (error != NULL)
*error = true;
if (info)
if (info != NULL)
*info = "Database error.";
return NULL;
}
Expand All @@ -83,7 +83,7 @@ char* postsrsd_forward(const char* addr, const char* domain, srs_t* srs,
return output;
}
free(output);
if (info)
if (info != NULL)
*info = srs_strerror(result);
log_info("<%s> not rewritten: %s", addr, srs_strerror(result));
return NULL;
Expand All @@ -93,14 +93,14 @@ char* postsrsd_reverse(const char* addr, srs_t* srs, database_t* db,
bool* error, const char** info)
{
char buffer[513];
if (error)
if (error != NULL)
*error = false;
if (info)
if (info != NULL)
*info = NULL;
int result = srs_reverse(srs, buffer, sizeof(buffer), addr);
if (result != SRS_SUCCESS)
{
if (info)
if (info != NULL)
*info = srs_strerror(result);
if (result != SRS_ENOTSRSADDRESS)
{
Expand All @@ -113,18 +113,18 @@ char* postsrsd_reverse(const char* addr, srs_t* srs, database_t* db,
return NULL;
}
const char* at = strchr(buffer, '@');
if (!at)
if (at == NULL)
{
log_info("<%s> not reversed: internal error", addr);
if (error)
if (error != NULL)
*error = true;
if (info)
if (info != NULL)
*info = "Internal error.";
return NULL;
}
if (strcmp(at, "@1") == 0)
{
if (db)
if (db != NULL)
{
char* p = buffer;
while (*p)
Expand All @@ -133,10 +133,10 @@ char* postsrsd_reverse(const char* addr, srs_t* srs, database_t* db,
++p;
}
char* sender = database_read(db, buffer);
if (!sender)
if (sender == NULL)
{
log_info("<%s> not reversed: unknown alias", addr);
if (info)
if (info != NULL)
*info = "Unknown alias.";
return NULL;
}
Expand Down
Loading

0 comments on commit 3aac97f

Please sign in to comment.