]> git.ipfire.org Git - pakfire.git/commitdiff
problem: Refactor generating problem string
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Jul 2021 16:35:00 +0000 (16:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 2 Jul 2021 16:35:00 +0000 (16:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/problem.c

index 9a00b2841f7b31eba8b9b194b2bd5c96c985ec9f..fc384e1eeac742d8dc271cf4302d47e154920742 100644 (file)
@@ -39,7 +39,7 @@ struct pakfire_problem {
        char* string;
 };
 
-static char* to_string(struct pakfire_problem* problem) {
+static char* pakfire_problem_make_string(struct pakfire_problem* problem) {
        Solver* solver = pakfire_request_get_solver(problem->request);
        Pool* pool = solver->pool;
 
@@ -53,154 +53,125 @@ static char* to_string(struct pakfire_problem* problem) {
 
        SolverRuleinfo type = solver_ruleinfo(solver, rule, &source, &target, &dep);
 
-       char s[1024];
+       char* s = NULL;
+       int r = 1;
+
        switch (type) {
                case SOLVER_RULE_DISTUPGRADE:
-                       snprintf(s, sizeof(s) - 1,
-                               _("%s does not belong to a distupgrade repository"),
-                               pool_solvid2str(pool, source)
-                       );
+                       r = asprintf(&s, _("%s does not belong to a distupgrade repository"),
+                               pool_solvid2str(pool, source));
                        break;
 
                case SOLVER_RULE_INFARCH:
-                       snprintf(s, sizeof(s) - 1,
-                               _("%s has inferior architecture"),
-                               pool_solvid2str(pool, source)
-                       );
+                       r = asprintf(&s, _("%s has inferior architecture"),
+                               pool_solvid2str(pool, source));
                        break;
 
                case SOLVER_RULE_UPDATE:
-                       snprintf(s, sizeof(s) - 1,
-                               _("problem with installed package %s"),
-                               pool_solvid2str(pool, source)
-                       );
+                       r = asprintf(&s, _("problem with installed package %s"),
+                               pool_solvid2str(pool, source));
                        break;
 
                case SOLVER_RULE_JOB:
-                       snprintf(s, sizeof(s) - 1, _("conflicting requests"));
+                       r = asprintf(&s, _("conflicting requests"));
                        break;
 
                case SOLVER_RULE_JOB_UNSUPPORTED:
-                       snprintf(s, sizeof(s) - 1, _("unsupported request"));
+                       r = asprintf(&s, _("unsupported request"));
                        break;
 
                case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
-                       snprintf(s, sizeof(s) - 1,
-                               _("nothing provides requested %s"),
-                               pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("nothing provides requested %s"), pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
-                       snprintf(s, sizeof(s) - 1, _("package %s does not exist"),
-                               pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("package %s does not exist"), pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
-                       snprintf(s, sizeof(s) - 1, _("%s is provided by the system"),
-                               pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("%s is provided by the system"), pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_RPM:
-                       snprintf(s, sizeof(s) - 1, _("some dependency problem"));
+                       r = asprintf(&s, _("some dependency problem"));
                        break;
 
                case SOLVER_RULE_BEST:
                        if (source > 0)
-                               snprintf(s, sizeof(s) - 1,
-                                       _("cannot install the best update candidate for package %s"),
-                                       pool_solvid2str(pool, source)
-                               );
+                               r = asprintf(&s, _("cannot install the best update candidate for package %s"),
+                                       pool_solvid2str(pool, source));
                        else
-                               snprintf(s, sizeof(s) - 1, _("cannot install the best candidate for the job"));
+                               r = asprintf(&s, _("cannot install the best candidate for the job"));
                        break;
 
                case SOLVER_RULE_RPM_NOT_INSTALLABLE:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s is not installable"),
-                               pool_solvid2str(pool, source)
-                       );
+                       r = asprintf(&s, _("package %s is not installable"),
+                               pool_solvid2str(pool, source));
                        break;
 
                case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
-                       snprintf(s, sizeof(s) - 1,
-                               _("nothing provides %s needed by %s"),
-                               pool_dep2str(pool, dep),  pool_solvid2str(pool, source)
-                       );
+                       r = asprintf(&s, _("nothing provides %s needed by %s"),
+                               pool_dep2str(pool, dep),  pool_solvid2str(pool, source));
                        break;
 
                case SOLVER_RULE_RPM_SAME_NAME:
-                       snprintf(s, sizeof(s) - 1,
-                               _("cannot install both %s and %s"),
-                               pool_solvid2str(pool, source),  pool_solvid2str(pool, target)
-                       );
+                       r = asprintf(&s, _("cannot install both %s and %s"),
+                               pool_solvid2str(pool, source),  pool_solvid2str(pool, target));
                        break;
 
                case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s conflicts with %s provided by %s"),
+                       r = asprintf(&s, _("package %s conflicts with %s provided by %s"),
                                pool_solvid2str(pool, source), pool_dep2str(pool, dep),
-                               pool_solvid2str(pool, target)
-                       );
+                               pool_solvid2str(pool, target));
                        break;
 
                case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s obsoletes %s provided by %s"),
+                       r = asprintf(&s, _("package %s obsoletes %s provided by %s"),
                                pool_solvid2str(pool, source), pool_dep2str(pool, dep),
-                               pool_solvid2str(pool, target)
-                       );
+                               pool_solvid2str(pool, target));
                        break;
 
                case SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES:
-                       snprintf(s, sizeof(s) - 1,
-                               _("installed package %s obsoletes %s provided by %s"),
+                       r = asprintf(&s, _("installed package %s obsoletes %s provided by %s"),
                                pool_solvid2str(pool, source), pool_dep2str(pool, dep),
-                               pool_solvid2str(pool, target)
-                       );
+                               pool_solvid2str(pool, target));
                        break;
 
                case SOLVER_RULE_RPM_IMPLICIT_OBSOLETES:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s implicitely obsoletes %s provided by %s"),
+                       r = asprintf(&s, _("package %s implicitely obsoletes %s provided by %s"),
                                pool_solvid2str(pool, source), pool_dep2str(pool, dep),
-                               pool_solvid2str(pool, target)
-                       );
+                               pool_solvid2str(pool, target));
                        break;
 
                case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s requires %s, but none of the providers can be installed"),
-                               pool_solvid2str(pool, source), pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("package %s requires %s, but none of the providers can be installed"),
+                               pool_solvid2str(pool, source), pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_RPM_SELF_CONFLICT:
-                       snprintf(s, sizeof(s) - 1,
-                               _("package %s conflicts with %s provided by itself"),
-                               pool_solvid2str(pool, source), pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("package %s conflicts with %s provided by itself"),
+                               pool_solvid2str(pool, source), pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_YUMOBS:
-                       snprintf(s, sizeof(s) - 1,
-                               _("both package %s and %s obsolete %s"),
-                               pool_solvid2str(pool, source), pool_solvid2str(pool, target), pool_dep2str(pool, dep)
-                       );
+                       r = asprintf(&s, _("both package %s and %s obsolete %s"),
+                               pool_solvid2str(pool, source), pool_solvid2str(pool, target),
+                               pool_dep2str(pool, dep));
                        break;
 
                case SOLVER_RULE_UNKNOWN:
                case SOLVER_RULE_FEATURE:
                case SOLVER_RULE_LEARNT:
                case SOLVER_RULE_CHOICE:
-                       snprintf(s, sizeof(s) - 1, _("bad rule type"));
+                       r = asprintf(&s, _("bad rule type"));
                        break;
-
        }
 
-       return strdup(s);
+       // Return nothing if asprintf failed
+       if (r < 0)
+               return NULL;
+
+       return s;
 }
 
 PAKFIRE_EXPORT int pakfire_problem_create(struct pakfire_problem** problem,
@@ -249,7 +220,7 @@ Pakfire pakfire_problem_get_pakfire(struct pakfire_problem* problem) {
 
 PAKFIRE_EXPORT const char* pakfire_problem_to_string(struct pakfire_problem* problem) {
        if (!problem->string)
-               problem->string = to_string(problem);
+               problem->string = pakfire_problem_make_string(problem);
 
        return problem->string;
 }