Skip to content

Commit

Permalink
#31: Run deepseek-coder-v2 over changed C files
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdope committed Jul 16, 2024
1 parent 97037bd commit 5dc323c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
20 changes: 10 additions & 10 deletions src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int pusb_device_connected(t_pusb_options *opts, UDisksClient *udisks)
GDBusObjectManager *manager = udisks_client_get_object_manager(udisks);
GList *objects = g_dbus_object_manager_get_objects(manager);
int retval = 0;
int i;
int i;
UDisksObject *object = NULL;
UDisksDrive *drive = NULL;

Expand All @@ -44,15 +44,15 @@ static int pusb_device_connected(t_pusb_options *opts, UDisksClient *udisks)

log_error("Searching for \"%s\" in the hardware database...\n", opts->device_list[currentDevice].name);

log_debug("Found %d drives to check/iterate...\n", g_list_length(objects));
log_debug("Found %d drives to check/iterate...\n", g_list_length(objects));
for (i = 0; i < g_list_length(objects); ++i)
{
object = UDISKS_OBJECT(g_list_nth(objects, i)->data);
if (udisks_object_peek_drive(object))
{
drive = udisks_object_get_drive(object);
retval = strcmp(udisks_drive_get_serial(drive), opts->device_list[currentDevice].serial) == 0;
log_debug("Looking for serial '%s', found '%s'...\n", opts->device_list[currentDevice].serial, udisks_drive_get_serial(drive));
log_debug("Looking for serial '%s', found '%s'...\n", opts->device_list[currentDevice].serial, udisks_drive_get_serial(drive));

if (strcmp(opts->device_list[currentDevice].vendor, "Generic") != 0)
{
Expand All @@ -66,12 +66,12 @@ static int pusb_device_connected(t_pusb_options *opts, UDisksClient *udisks)

g_object_unref(drive);
if (retval) {
strcpy(opts->device.name, opts->device_list[currentDevice].name);
strcpy(opts->device.vendor, opts->device_list[currentDevice].vendor);
strcpy(opts->device.model, opts->device_list[currentDevice].model);
strcpy(opts->device.serial, opts->device_list[currentDevice].serial);
strcpy(opts->device.volume_uuid, opts->device_list[currentDevice].volume_uuid);
currentDevice = 11;
strncpy(opts->device.name, opts->device_list[currentDevice].name, sizeof(opts->device.name) - 1);

Check failure

Code scanning / devskim

strncpy is dangerous, as if the source contains 'n' or more characters, it will not null terminate the destination. Error

Banned C function detected (strncpy)
strncpy(opts->device.vendor, opts->device_list[currentDevice].vendor, sizeof(opts->device.vendor) - 1);

Check failure

Code scanning / devskim

strncpy is dangerous, as if the source contains 'n' or more characters, it will not null terminate the destination. Error

Banned C function detected (strncpy)
strncpy(opts->device.model, opts->device_list[currentDevice].model, sizeof(opts->device.model) - 1);

Check failure

Code scanning / devskim

strncpy is dangerous, as if the source contains 'n' or more characters, it will not null terminate the destination. Error

Banned C function detected (strncpy)
strncpy(opts->device.serial, opts->device_list[currentDevice].serial, sizeof(opts->device.serial) - 1);

Check failure

Code scanning / devskim

strncpy is dangerous, as if the source contains 'n' or more characters, it will not null terminate the destination. Error

Banned C function detected (strncpy)
strncpy(opts->device.volume_uuid, opts->device_list[currentDevice].volume_uuid, sizeof(opts->device.volume_uuid) - 1);

Check failure

Code scanning / devskim

strncpy is dangerous, as if the source contains 'n' or more characters, it will not null terminate the destination. Error

Banned C function detected (strncpy)
currentDevice = 10;
break;
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ int pusb_device_check(t_pusb_options *opts, const char *user)
if (udisks_client_error != NULL)
{
log_error("Unable to check for device, could not get UDisksClient! Error was: %s\n", udisks_client_error->message);
g_error_free (udisks_client_error);
g_error_free(udisks_client_error);
return (0);
}

Expand Down
78 changes: 49 additions & 29 deletions src/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ static xmlXPathObject *pusb_xpath_match(xmlDocPtr doc, const char *path)
if (context == NULL)
{
log_error("Unable to create XML context\n");
return (NULL);
return NULL;
}
result = xmlXPathEvalExpression((xmlChar *)path, context);
xmlXPathFreeContext(context);
if (result == NULL)
{
log_error("Error in xmlXPathEvalExpression\n");
return (NULL);
return NULL;
}
if (xmlXPathNodeSetIsEmpty(result->nodesetval))
{
xmlXPathFreeObject(result);
return (NULL);
return NULL;
}
return (result);
return result;
}

static int pusb_xpath_strip_string(char *dest, const char *src, size_t size)
Expand All @@ -71,18 +71,18 @@ static int pusb_xpath_strip_string(char *dest, const char *src, size_t size)

if (first_char == -1 || last_char == -1)
{
return (0);
return 0;
}

if ((last_char - first_char) > (size - 1))
{
log_error("Device name is too long: %s", src);
return (0);
return 0;
}

memset(dest, 0x0, size);
strncpy(dest, &(src[first_char]), last_char - first_char + 1);
return (1);
return 1;
}

int pusb_xpath_get_string(
Expand All @@ -98,14 +98,14 @@ int pusb_xpath_get_string(

if (!(result = pusb_xpath_match(doc, path)))
{
return (0);
return 0;
}

if (result->nodesetval->nodeNr > 1)
{
xmlXPathFreeObject(result);
log_debug("Syntax error: %s: more than one record found\n", path);
return (0);
return 0;
}

node = result->nodesetval->nodeTab[0]->xmlChildrenNode;
Expand All @@ -114,18 +114,18 @@ int pusb_xpath_get_string(
{
xmlXPathFreeObject(result);
log_debug("Empty value for %s\n", path);
return (0);
return 0;
}
if (!pusb_xpath_strip_string(value, (const char *)result_string, size))
{
xmlFree(result_string);
xmlXPathFreeObject(result);
log_debug("Result for %s (%s) is too long (max: %d)\n", path, (const char *)result_string, size);
return (0);
return 0;
}
xmlFree(result_string);
xmlXPathFreeObject(result);
return (1);
return 1;
}

int pusb_xpath_get_string_list(
Expand All @@ -141,7 +141,7 @@ int pusb_xpath_get_string_list(

if (!(result = pusb_xpath_match(doc, path)))
{
return (0);
return 0;
}

log_error("DBG: Found %d devices for user\n", result->nodesetval->nodeNr);
Expand All @@ -150,7 +150,7 @@ int pusb_xpath_get_string_list(
log_error("DBG: result %d\n", currentResult);
node = result->nodesetval->nodeTab[currentResult]->xmlChildrenNode;
result_string = xmlNodeListGetString(doc, node, 1);
if (!result_string || strcmp("", (char *) result_string) == 0)
if (!result_string || strcmp("", (char *)result_string) == 0)
{
log_debug("Empty value for %s\n", path);
continue;
Expand All @@ -166,7 +166,7 @@ int pusb_xpath_get_string_list(

xmlFree(result_string);
xmlXPathFreeObject(result);
return (1);
return 1;
}

int pusb_xpath_get_string_from(
Expand All @@ -183,6 +183,11 @@ int pusb_xpath_get_string_from(

xpath_size = strlen(base) + strlen(path) + 1;
xpath = xmalloc(xpath_size);
if (xpath == NULL)
{
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_string(doc, xpath, value, size);
Expand All @@ -192,7 +197,7 @@ int pusb_xpath_get_string_from(
}

xfree(xpath);
return (retval);
return retval;
}

int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value)
Expand All @@ -201,23 +206,23 @@ int pusb_xpath_get_bool(xmlDocPtr doc, const char *path, int *value)

if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret)))
{
return (0);
return 0;
}

if (!strcmp(ret, "true"))
{
*value = 1;
return (1);
return 1;
}

if (!strcmp(ret, "false"))
{
*value = 0;
return (1);
return 1;
}

log_debug("Expecting a boolean, got %s\n", ret);
return (0);
return 0;
}

int pusb_xpath_get_bool_from(
Expand All @@ -233,11 +238,16 @@ int pusb_xpath_get_bool_from(

xpath_size = strlen(base) + strlen(path) + 1;
xpath = xmalloc(xpath_size);
if (xpath == NULL)
{
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_bool(doc, xpath, value);
xfree(xpath);
return (retval);
return retval;
}

int pusb_xpath_get_time(xmlDocPtr doc, const char *path, time_t *value)
Expand All @@ -248,7 +258,7 @@ int pusb_xpath_get_time(xmlDocPtr doc, const char *path, time_t *value)

if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret)))
{
return (0);
return 0;
}

last = &(ret[strlen(ret) - 1]);
Expand All @@ -272,16 +282,16 @@ int pusb_xpath_get_time(xmlDocPtr doc, const char *path, time_t *value)
else if (!isdigit(*last))
{
log_debug("Expecting a time modifier, got %c\n", *last);
return (0);
return 0;
}
if (!isdigit(*last))
{
*last = '\0';
}

*value = (time_t) atoi(ret) * coef;
*value = (time_t)atoi(ret) * coef;

Check warning

Code scanning / devskim

These functions are historically error-prone and have been associated with a significant number of vulnerabilities. Most of these functions have safer alternatives, such as replacing 'strcpy' with 'strlcpy' or 'strcpy_s'. Warning

Banned C function detected

return (0);
return 1;
}

int pusb_xpath_get_time_from(
Expand All @@ -297,11 +307,16 @@ int pusb_xpath_get_time_from(

xpath_size = strlen(base) + strlen(path) + 1;
xpath = xmalloc(xpath_size);
if (xpath == NULL)
{
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_time(doc, xpath, value);
xfree(xpath);
return (retval);
return retval;
}

int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value)
Expand All @@ -310,11 +325,11 @@ int pusb_xpath_get_int(xmlDocPtr doc, const char *path, int *value)

if (!pusb_xpath_get_string(doc, path, ret, sizeof(ret)))
{
return (0);
return 0;
}

*value = atoi(ret);
return (1);
return 1;
}

int pusb_xpath_get_int_from(
Expand All @@ -330,9 +345,14 @@ int pusb_xpath_get_int_from(

xpath_size = strlen(base) + strlen(path) + 1;
xpath = xmalloc(xpath_size);
if (xpath == NULL)
{
log_error("Memory allocation failed\n");
return 0;
}
memset(xpath, 0x00, xpath_size);
snprintf(xpath, xpath_size, "%s%s", base, path);
retval = pusb_xpath_get_int(doc, xpath, value);
xfree(xpath);
return (retval);
return retval;
}

0 comments on commit 5dc323c

Please sign in to comment.